比较标签中的单元格

时间:2018-03-20 12:09:36

标签: excel vba if-statement compare cells

我正在尝试比较2个标签(主要和测试)中的单元格,如果测试中有变化,那么颜色会发生变化,并且会出现任何颜色和副本。将其粘贴到主文件中。

更新:

这是必需的代码

Sub test()

    Dim varSheetA As Variant
    Dim varSheetB As Variant
    Dim strRangeToCheck As String
    Dim iRow As Long
    Dim iCol As Long
        Dim jRow As Long
    Dim jCol As Long

    strRangeToCheck = "A1:V1000"
    ' If you know the data will only be in a smaller range, reduce the size of the ranges above.
    Debug.Print Now
    varSheetA = Worksheets("Sheet1").Range(strRangeToCheck)
    varSheetB = Worksheets("Sheet2").Range(strRangeToCheck) ' or whatever your other sheet is.
    Debug.Print Now

    For iRow = LBound(varSheetA, 1) To UBound(varSheetA, 1)
        For iCol = LBound(varSheetA, 2) To UBound(varSheetA, 2)
            If varSheetB(iRow, iCol) = varSheetA(iRow, iCol) Then
                ' Cells are identical.
                ' Do nothing.
            Else

            Sheets("Sheet1").Select
            Cells(iRow, iCol).Interior.ColorIndex = 44
            Sheets("Sheet2").Select
            Cells(iRow, iCol).Interior.ColorIndex = 44


            Sheets("Sheet2").Select
            Cells(iRow, iCol).Copy
            Sheets("Sheet1").Select
            Cells(iRow, iCol).PasteSpecial xlValues
            Cells(iRow, iCol).PasteSpecial xlFormats

                ' Cells are different.
                ' Code goes here for whatever it is you want to do.
            End If
        Next iCol
    Next iRow
MsgBox ("Done")
End Sub

1 个答案:

答案 0 :(得分:0)

在其中一个工作表中找到最后使用过的单元格。

dim lr as long, lc as long

lr= application.max(dWS.cells.specialcells(xlCellTypeLastCell).row, _
                    mWS.cells.specialcells(xlCellTypeLastCell).row)
lc= application.max(dWS.cells.specialcells(xlCellTypeLastCell).Column, _
                    mWS.cells.specialcells(xlCellTypeLastCell).Column)

For Each c In dWS.Range("A2", dWS.cells(lr, lc))
    If Not dWS.Cells(c.Row, c.Column).Value = mWS.Cells(c.Row, c.Column).Value Then
        dWS.Cells(c.Row, c.Column).Interior.Color = vbYellow
    End If
Next
相关问题