[英]If cell value in table range equals “New” copy entire row and paste as values in sheet 1 in the next empty cell
I am trying to do a basic thing but cannot get it right.我正在尝试做一件基本的事情,但无法做到正确。
I want to evaluate the cells on sheet 2(New Roster) in a table column(OldNew) for the value "New".我想评估表列(OldNew)中工作表 2(New Roster)上的单元格的值“New”。 If it has the value, copy the entire row and add it to the table(CurrentRoster) on sheet 1(Current Roster).
如果它具有该值,则复制整行并将其添加到工作表 1(当前名册)上的表(当前名册)中。
Here is the code I am using:这是我正在使用的代码:
For Each c In wb.Names("OldNew").RefersToRange.Cells
If c.Value Like "New" Then
On Error Resume Next
Set SourceTable = Worksheets("New Roster").ListObjects("NewRoster").DataBodyRange
Set DestinationTable = Worksheets("Current Roster").ListObjects("CurrentRoster").ListRows.Add
SourceTable.Copy
DestinationTable.Range.PasteSpecial xlPasteValues
End If
Next
This endlessly loops and does not do what I want.这无休止地循环并且不做我想要的。
Here is the entire code for context: Sub TableData()这是上下文的完整代码:Sub TableData()
Dim tbl As ListObject Dim cell As Range Dim rng As Range Dim RangeName As String Dim CellName As String Dim wb As Workbook, c As Range, m Dim ws1 As Worksheet Dim lr As Long Dim lo As ListObject Dim SourceTable Dim DestinationTable Dim tbl As ListObject Dim cell As Range Dim rng As Range Dim RangeName As String Dim CellName As String Dim wb As Workbook, c As Range, m Dim ws1 As Worksheet Dim lr As Long Dim lo As ListObject Dim SourceTable Dim DestinationTable
Worksheets("New Roster").Activate Range("A1").Select工作表(“新花名册”)。激活范围(“A1”)。Select
If Range("A1") = "" Then
MsgBox "No Data to Reconcile"
Exit Sub
Else
End If
Application.ScreenUpdating = False '---->Prevents screen flickering as the code executes.
Application.DisplayAlerts = False '---->Prevents warning "pop-ups" from appearing.
' Clears hidden columns from previous user
Worksheets("Current Roster").Activate
Range("A1").Activate
Columns.EntireColumn.Hidden = False
On Error Resume Next
Sheet1.ShowAllData
On Error GoTo 0
' Tables the New Roster
Worksheets("New Roster").Activate
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name _
= "NewRoster"
Range("NewRoster[#All]").Select
ActiveSheet.ListObjects("NewRoster").TableStyle = ""
' Name Ranges for Reference, New Name List From New Roster
ActiveSheet.Range("F2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="NewNameList", RefersToR1C1:= _
"=NewRoster[Member AHCCCS ID]"
ActiveWorkbook.Names("NewNameList").Comment = "Contains New list to compare old list to"
' Compares CurrentNameList Values to NewNameList Values to verify if current names are still active
Set wb = ThisWorkbook
For Each c In wb.Names("CurrentNameList").RefersToRange.Cells
m = Application.Match(c.Value, wb.Names("NewNameList").RefersToRange, 0)
c.Offset(0, 26).Value = IIf(IsError(m), "InActive", "Active")
Next c
' Adds Column to New Roster Table and place Old/New in header cell
Worksheets("New Roster").Activate
Worksheets("New Roster").Range("AF1").Value = "Old/New"
' Names Old/New Range
ActiveSheet.Range("AF1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="OldNew", RefersToR1C1:= _
"=NewRoster[Old/New]"
ActiveWorkbook.Names("OldNew").Comment = ""
' Compares CurrentNameList Values to NewNameList Values to determine if New Name, If so, Add to Current
Roster
For Each c In wb.Names("NewNameList").RefersToRange.Cells
m = Application.Match(c.Value, wb.Names("CurrentNameList").RefersToRange, 0)
c.Offset(0, 26).Value = IIf(IsError(m), "New", "Old")
Next c
' Move Rows with "New" from New Roster to Current Roster Worksheet
Worksheets("New Roster").Activate
For Each c In wb.Names("OldNew").RefersToRange.Cells
If c.Value Like "New" Then
On Error Resume Next
Set SourceTable = Worksheets("New Roster").ListObjects("NewRoster").DataBodyRange
Set DestinationTable = Worksheets("Current Roster").ListObjects("CurrentRoster").ListRows.Add
SourceTable.Copy
DestinationTable.Range.PasteSpecial xlPasteValues
End If
Next
' Clear New Roster Data
Worksheets("New Roster").Activate
Columns("A:A").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlToLeft
ActiveWorkbook.Names("NewNameList").Delete
ActiveWorkbook.Names("OldNew").Delete
Worksheets("Current Roster").Activate
Range("A1").Activate
ActiveSheet.Range("CurrentRoster[#All]").RemoveDuplicates Columns:=Array(1, 2, _
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31 _
, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55), _
Header:=xlYes
Application.DisplayAlerts = True '---->Resets the default.
Application.ScreenUpdating = True '---->Resets the default.
End Sub
NewRoster
has a column with the header OldNew
.NewRoster
具有包含 header OldNew
的列时才有效。vbTextCompare
as the fourth argument in the Instr
function.vbTextCompare
作为Instr
function 中的第四个参数。On Error Resume Next
.On Error Resume Next
。 Approximately: It is usually (exclusively) used on one (a few) lines and is 'ended' with an On Error Goto 0
or with some error handling, eg If Err then
which will again contain On Error Goto 0
or some other On Error
statement.On Error Goto 0
或一些错误处理“结束”,例如If Err then
将再次包含On Error Goto 0
或其他一些On Error
陈述。 There's a lot more to it. The Code编码
Sub copyFromTableToTable()
Dim wb As Workbook
Set wb = ThisWorkbook
With wb.Worksheets("New Roster").ListObjects("NewRoster")
Dim c As Range
Dim dest As Range
Dim hRow As Long
hRow = .HeaderRowRange.Row
For Each c In .ListColumns("OldNew").DataBodyRange
If InStr(1, c.Value, "New") > 0 Then
With wb.Worksheets("Current Roster").ListObjects("CurrentRoster")
' This doesn't work.
'Set dest = .ListRows.Add
.ListRows.Add
With .DataBodyRange
Set dest = .Rows(.Rows.Count)
End With
End With
dest.Value = .DataBodyRange.Rows(c.Row - hRow).Value
End If
Next c
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.