【发布时间】:2023-03-18 06:25:02
【问题描述】:
我在这里做了一些代码来匹配一张纸上的一系列单元格(CRD),将它们与另一张纸上的一系列单元格(PRD)进行比较,并突出显示第一张纸的 A 列中的单元格( PRD) 具有匹配范围。
我还想将与第二张工作表 (PRD) 不匹配的范围复制并粘贴到第三张工作表 (工作表 1) 上的第一张工作表 (CRD) 的 A 列中的范围。我确信这是我正在使用的结束和退出以及下一个语句的结构,但在广泛使用谷歌搜索后我无法弄清楚这一点。感谢您对我的问题/问题的任何帮助或批评。
Sub Loop_Test()
Dim compareRange As Range, toCompare As Range
Dim lastRow1 As Long, lastRow2 As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Dim i As Long, j As Long, K As Long, L As Long
Dim PasteRow As Long
Dim wsDest As Worksheet
Set ws1 = ThisWorkbook.Worksheets("PRD")
Set ws2 = ThisWorkbook.Worksheets("CRD")
lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
Set wsReport = ThisWorkbook.Worksheets("Sheet1")
Set wsSrc = ActiveSheet
Set compareRange = ws1.Range("A" & lastRow1)
Set toCompare = ws2.Range("A" & lastRow2)
For i = 2 To lastRow2
For j = 2 To lastRow1
If ws2.Cells(i, 1) = ws1.Cells(j, 1) _
And ws2.Cells(i, 2) = ws1.Cells(j, 2) _
And ws2.Cells(i, 3) = ws1.Cells(j, 3) _
And ws2.Cells(i, 4) = ws1.Cells(j, 4) _
And ws2.Cells(i, 5) = ws1.Cells(j, 5) _
And ws2.Cells(i, 6) = ws1.Cells(j, 6) Then
ws2.Cells(i, 1).Interior.Color = vbGreen
Else
For K = 2 To lastRow2
For L = 2 To lastRow1
If ws2.Cells(K, 1) <> ws1.Cells(L, 1) _
And ws2.Cells(K, 2) <> ws1.Cells(L, 2) _
And ws2.Cells(K, 3) <> ws1.Cells(L, 3) _
And ws2.Cells(K, 4) <> ws1.Cells(L, 4) _
And ws2.Cells(K, 5) <> ws1.Cells(L, 5) _
And ws2.Cells(K, 6) <> ws1.Cells(L, 6) Then
Set wsDest = wsReport
With wsDest
wsSrc.Rows(i).Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With
End If
Exit For
Next L
Exit For
Next K
End If
Next j
Next i
结束子
【问题讨论】: