我是VBA的新手,希望能帮助您在Excel中创建一个宏。
我有一个我希望比较的两张纸的文件。我需要将Sheet 1 Column G(序列号)与Sheet 2 Column C(序列号)进行比较,如果Sheet 2 Column K = D(Disposed Status),则将Sheet 1 Column G高亮显示颜色。
我在另一个线程上找到了这个代码,但无法得到"和"部分工作。
**Sub Compare2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet 2")
For Each i In ws2.Range("G2:G30")
For Each C In ws1.Range("C2:C50")
For Each L In ws2.Range("K2:K50")
If i.Cells.Value = C.Cells.Value And L.Cells.Value = "D" Then
i.Cells.Interior.ColorIndex = 3
End If
Next
Next
Next
End Sub**
答案 0 :(得分:0)
以不同的风格试试..
Sub Compare2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim x,y as Integer
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")
For x = 2 to 30
For y = 2 to 50
If ws1.cells(x,7) = ws2.cells(y,3) and ws2.cells(y,11) = "D" Then
ws1.Cells(x,7).Interior.ColorIndex = 3
End if
Next
Next
End Sub
答案 1 :(得分:0)
尝试将另一个IF语句嵌套到其中
**Sub Compare2()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet 2")
For Each i In ws2.Range("G2:G30")
For Each C In ws1.Range("C2:C50")
For Each L In ws2.Range("K2:K50")
If i.Cells.Value = C.Cells.Value Then
If L.Cells.Value = "D" Then
i.Cells.Interior.ColorIndex = 3
End If
End if
Next
Next
Next
End Sub**`