简体   繁体   English

如果表格范围内的单元格值等于“新建”,则复制整行并粘贴为工作表 1 中下一个空单元格中的值

[英]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

Copy From Table to Table从表复制到表

  • Remove the duplicate declarations and references.删除重复的声明和引用。
  • This will only work if both tables have the same number of columns and table NewRoster has a column with the header OldNew .这仅在两个表具有相同数量的列并且表NewRoster具有包含 header OldNew的列时才有效。
  • It's a standalone version, so you can test it as is.这是一个独立版本,因此您可以按原样进行测试。 Later you just have to delete the added rows.稍后您只需删除添加的行。
  • If you want to allow case-insensitivity (allow new,NEW), you can add vbTextCompare as the fourth argument in the Instr function.如果要允许不区分大小写(允许新,新),可以添加vbTextCompare作为Instr function 中的第四个参数。
  • Forget about 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.还有很多。 You should know exactly why you are using 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.

相关问题 VBA 将范围复制到最后一行并粘贴到新工作表上,单元格 A19 之后的第一个空行 - VBA copy range to last row & paste on new sheet, first empty row after cell A19 在另一张纸上查找匹配值并粘贴到该行的下一个空单元格中 - Find matching value on another sheet and paste into next empty cell on that row 从范围复制并过去到下一个空单元格中的另一个工作表中 - Copy from a range and past in another sheet in the next empty cell in a row 仅当单元格等于工作表/选项卡名称时,如何复制和粘贴整行? - How do I copy and paste entire row only when cell equals sheet/tab name? 如何在下一个空单元格中复制和粘贴动态表值? - How to copy and paste dynamic table values in next empty cell? 尝试从一张纸上复制一个范围并将其粘贴到另一张纸上一列中的下一个空单元格 - Trying to copy a range from one sheet and paste it to the next empty cell in a column on another sheet VBA值粘贴到范围中的下一个空单元格 - VBA Paste Value to Next Empty Cell in a Range 将整行复制到新工作表并根据单元格值更改单元格值 - Copy entire row to a new sheet and change cell value based on cell value 复制并粘贴到下一个空单元格 - Copy & Paste to the next empty cell 如何在不同的单元格范围内将具有不同值的整行复制并粘贴到新表中? - How can I copy & paste entire rows with distinct values to a new sheet on varying cell ranges?
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM