Excel VBA代码比较两个表并突出显示不匹配

时间:2016-03-28 09:34:39

标签: excel vba excel-vba

我一直在尝试编写一个代码,用于比较两个相同大小的表中的值,并突出显示不匹配的值。我想出的代码似乎只是突出显示该范围的最后一个单元格。我想我在这里忽略了一些非常简单的事情。任何帮助解决这个问题将非常感激。我正在记下下面的代码。

Sub Compare_Table()

Dim oldTable As Range, newTable As Range, i As Integer, j As Integer, m As Integer, n As Integer
Set oldTable = Application.InputBox(Prompt:="Please Select old values", Title:="Range Select", Type:=8)
Set newTable = Application.InputBox(Prompt:="Please Select new values", Title:="Range Select", Type:=8)

i = oldTable.Rows.Count
j = oldTable.Columns.Count


For m = 1 To i
    For n = 1 To j
        If oldTable.Cells(i, j) = newTable.Cells(i, j) Then
            newTable.Cells(i, j).Interior.ColorIndex = 6
        Else
            newTable.Cells(i, j).Interior.ColorIndex = 3
        End If

    Next n
Next m

End Sub

1 个答案:

答案 0 :(得分:0)

在for循环中进行以下更改:

For m = 1 To i
    For n = 1 To j
        If oldTable.Cells(m, n) = newTable.Cells(m, n) Then
            newTable.Cells(m, n).Interior.ColorIndex = 6
        Else
            newTable.Cells(m, n).Interior.ColorIndex = 3
        End If

    Next n
Next m
相关问题