2016-03-28 129 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

相關問題