用于跟踪更改的VBA代码

时间:2018-02-03 11:51:42

标签: vba excel-vba excel

我需要一个VBA代码,可以跟踪OLD表格和值不是由“单元格”组成的新工作表,我们在两个工作表中的“唯一”值都在" Z"列中。

如果找到任何新的号码,它应突出显示为新的订单编号

This是工作表截图,其中Z列包含我试图与旧工作表进行比较的唯一编号,但我失败了。

3 个答案:

答案 0 :(得分:2)

以下内容将符合您的期望:

Sub foo()
Dim wsNew As Worksheet: Set wsNew = Sheets("NEW")
Dim wsOld As Worksheet: Set wsOld = Sheets("OLD")
'above declare and set both your worksheets, amend as required
Dim Result As String

OldLastRow = wsOld.Cells(wsOld.Rows.Count, "Z").End(xlUp).Row
NewLastRow = wsNew.Cells(wsNew.Rows.Count, "Z").End(xlUp).Row
'above get the number of rows with data on each sheet on column Z

For i = 2 To NewLastRow 'loop through New sheet from row 2 to last
    SearchValue = wsNew.Cells(i, "Z") 'get the value of that row
    For x = 2 To OldLastRow 'loop through Old sheet to find a match
        If wsOld.Cells(x, "Z") = SearchValue Then Result = "Found" 'if match found, set Result variable as flag
    Next x
    If Result = "Found" Then 'if found
        Result = "" 'remove flag before next loop
    Else 'if not found
        wsNew.Rows(i).EntireRow.Interior.Color = 5296274 'highlight the row
    End If
Next i
End Sub

<强>更新

如果不是突出显示该行,而是希望将该行复制到另一个工作表,则以下内容会将该行复制到Sheet4,根据需要修改工作表名称:

Sub foo()
Dim wsNew As Worksheet: Set wsNew = Sheets("NEW")
Dim wsOld As Worksheet: Set wsOld = Sheets("OLD")
Dim wsResult As Worksheet: Set wsResult = Sheets("Sheet4")
'above declare and set both your worksheets, amend as required
Dim Result As String
Dim i As Long
Dim x As Long

OldLastRow = wsOld.Cells(wsOld.Rows.Count, "Z").End(xlUp).Row
NewLastRow = wsNew.Cells(wsNew.Rows.Count, "Z").End(xlUp).Row
'above get the number of rows with data on each sheet on column Z

For i = 2 To NewLastRow 'loop through New sheet from row 2 to last
    SearchValue = wsNew.Cells(i, "Z") 'get the value of that row
    For x = 2 To OldLastRow 'loop through Old sheet to find a match
        If wsOld.Cells(x, "Z") = SearchValue Then Result = "Found" 'if match found, set Result variable as flag
    Next x
    If Result = "Found" Then 'if found
        Result = "" 'remove flag before next loop
    Else 'if not found
        NextFreeRow = wsResult.Cells(wsResult.Rows.Count, "A").End(xlUp).Row + 1
        wsNew.Rows(i).EntireRow.Copy Destination:=wsResult.Cells(NextFreeRow, 1)
    End If
Next i
End Sub

答案 1 :(得分:0)

我对您的代码进行了一些更改请检查,它没有显示预期结果,而是显示错误。

Dim wsNew As Worksheet: Set wsNew = Sheets("NEW")
Dim wsOld As Worksheet: Set wsOld = Sheets("OLD")
Dim result As Range Dim SearchValue, i, x As Integer 
Dim OldLastRow As Long Dim NewLastRow As Long
Set result = ThisWorkbook.Sheets("sheet4").Range("a2").End(xlDown)
OldLastRow = wsOld.Cells(wsOld.Rows.Count, "Z").End(xlUp).Row 
NewLastRow = wsNew.Cells(wsNew.Rows.Count, "Z").End(xlUp).Row 
For i = 2 To NewLastRow 'loop through New sheet from row 2 to last
SearchValue = wsNew.Cells(i, "Z") 'get the value of that row
For x = 2 To OldLastRow 'loop through Old sheet to find a match
If wsOld.Cells(x, "Z") = SearchValue Then_
result = wsOld.Rows(x).EntireRow 
Next x
If result = wsOld.Rows(x).EntireRow Then  'if found
result.Interior.Color = 123456
Else 'if not found
    wsNew.Rows(i).EntireRow.Interior.Color = 5296274 'highlight the row
End If
Next i
End Sub

答案 2 :(得分:0)

请检查附件,此工作簿中的表格OLD包含旧数据&amp; NEW包含新的更新数据,实际上我需要在任何地方跟踪更改,因为我试图连接列#34; B&#34; &安培; &#34; F&#34;在专栏&#34; Z&#34;得到唯一的no.s所以使用那个参考我可以在OLD表中找到它&amp;如果找到则从两张纸上复制整行(NEW&amp; OLD)&amp;然后通过单元格跟踪更改,但它不起作用。因为有时候会发生变化,而且&#34; N&#34;即所需的数量,所以我无法跟踪它们。&amp;如果新行将添加到新表中,则它将显示为新订单。

为此,我尝试将匹配成立的行复制到不同的表格上。没有创建行也&amp;然后通过比较细胞尝试跟踪更改,但我失败了,你能不能给我一些更好的建议。

enter image description here在此输入图片说明 enter image description here