如何将ID与vba的2个工作表进行比较

时间:2017-06-15 07:05:42

标签: vba

我一直在尝试为我的项目创建一个vba代码。我需要检查ID中特定行中的1个单元格是否比其他工作表中具有相同ID的1个单元格具有更大或更小的值!

我有以下代码,直到将d值设置为8:

    Sub Compare_ID_ProductCode()

    Dim w1 As Worksheet, w2 As Worksheet
    Dim c As Range, a As Range, b As Range, d As Integer
    Set w1 = Sheets("Sheet1")
    Set w2 = Sheets("Sheet2")
    For Each c In w1.Range("C2", w1.Range("C" & Rows.Count).End(xlUp))
        Set a = w2.Columns(1).Find(c.Value, lookAt:=xlWhole)
        If Not a Is Nothing Then
            If w1.Cells(c.row, 3).Value = w2.Cells(a.row, 1).Value Then
                d = 8
                For Each a In w2.Range("Columns(6)", w2.Range("a.Rows" & Columns.Count).End(xlUp))
                    Set b = w1.Rows(c).Find(e.Value, lookAt:=xlWhole)
                    If Not b Is Nothing Then
                        If w1.Cells(c.row, Columns(d)).Value > w2.Cells(b.row, Columns(d)).Value Then
                            w1.Cells(c.row, Columns(d)).Cell.Color = vbGreen
                        End If

                        If w1.Cells(c.row, Columns(d)).Value < w2.Cells(b.row, Columns(d)).Value Then
                            w1.Cells(c.row, Columns(d)).Cell.Color = vbRed
                        End If

                    End If
                    d = d + 1
                Next a
            End If
        End If
    Next c
    End Sub

当我运行它时,它显示(方法&#39;范围&#39;对象&#34; _Worksheet&#34;失败)错误? 我该怎么做?

谢谢你的每一个提示都有帮助!

-Reijo

0 个答案:

没有答案