我试图比较两张Sheet1和sheet2以及sheet3中的打印值。当我在sheet1和sheet2之间进行比较并寻找不匹配的数字时,不知怎的,我的循环并没有停止,而且很多时候它的打印数量相同。这是我的代码,但有没有其他方法可以在两张纸之间找到不匹配的数字并将其粘贴到sheet3中。
lastrow1 = Sheets("Sheet1").UsedRange.Row - 1 + Sheets("Sheet1").UsedRange.Rows.Count
lastrow2 = Sheets("Sheet2").UsedRange.Row - 1 + Sheets("Sheet2").UsedRange.Rows.Count
a = 1
b = 1
c = 1
For i = a To lastrow1
For ii = b To lastrow2
If Worksheets("Sheet1").Cells(i, 1) <> Worksheets("Sheet2").Cells(ii, 1) Then
Worksheets("Sheet3").Range("A" & x) = Worksheets("Sheet1").Cells(i, 1)
x = x + 1
End If
Next ii
Next i
答案 0 :(得分:0)
注意:下面的代码检查Sheet1中的A列与Sheet2中的A列。然后,Main()
以相反的顺序调用相同的代码,以便针对Sheet1列A检查A列中Sheet2中的所有数字。如果您只想查看Sheet1中但不在Sheet2中注释掉的值第二次调用PrintNonMatching
Main()
Sub Main()
PrintNonMatching "Sheet1", "Sheet2", "Sheet3"
PrintNonMatching "Sheet2", "Sheet1", "Sheet3"
End Sub
Sub PrintNonMatching(arg1 As String, arg2 As String, arg3 As String)
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Set sh1 = Sheets(arg1): Set sh2 = Sheets(arg2): Set sh3 = Sheets(arg3)
Dim match As Boolean
For Each c1 In sh1.Range("A1:A" & sh1.Range("A" & Rows.Count).End(xlUp).Row)
For Each c2 In sh2.Range("A1:A" & sh2.Range("A" & Rows.Count).End(xlUp).Row)
If c1 = c2 Then match = True
Next
If Not match Then
sh3.Range("A" & sh3.Range("A" & Rows.Count).End(xlUp).Row + 1) = c1
End If
match = False
Next
End Sub