2016-03-28 127 views
0

我一直在试图编写一个代码,比较两个相同大小的表中的值,并突出显示不匹配的值。我提出的代码似乎只是突出了该范围的最后一个单元。我想我在这里可以忽略一些非常简单的事情。任何帮助来解决这个问题将不胜感激。我正在放下下面的代码。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 
+0

它更加高效的使用条件格式,可以直接在Excel或从VBA模块。在后一种情况下,Jeeped会回复一个相当新颖的帖子,它会让你走上正确的道路 – user3598756

回答

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 
+0

哇!感谢百万Mrig!我现在觉得非常愚蠢! – ExcelNoob

+0

很高兴我能帮到你。 – Mrig

相关问题