0

我是 VBA 新手,希望在 Excel 中创建宏得到一些帮助。

我有一个文件,里面有两张我想比较的文件。我需要将工作表 1 列 G(序列号)与工作表 2 列 C(序列号)进行比较,如果工作表 2 列 K = D(已处理状态),则突出显示工作表 1 列 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**
4

2 回答 2

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**`
于 2013-06-28T12:58:09.040 回答
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
于 2013-06-28T12:57:15.657 回答