評論說,也有類似的問題之前,我已經嘗試過他們,但他們沒有工作不幸Excel中,比較在兩片兩個具體的行和突出差異
嗨,這是我第一次就這樣,放心吧,我花了數小時尋找解決方案。我有一個狀態欄顯示狀態,如刪除,新建,更改。 狀態爲「更改」時,我想將E列中的特定行與Sheet3中Excel(XFD)中的最後一列相比較,將A列中的最後一列與Sheet1中Excel(XFD)中最後一個可能的列進行比較,並突出顯示細胞是不同的。
我發現這個解決方案: -
Dim diffB As Boolean
Dim r As Long, c As Integer, m As Integer
Dim lr1 As Long, lr2 As Long, lc1 As Integer, lc2 As Integer
Dim maxR As Long, maxC As Integer, cf1 As String, cf2 As String
Dim rptWB As Workbook, DiffCount As Long
Application.ScreenUpdating = False
Application.StatusBar = "Creating the report..."
Application.DisplayAlerts = True
With Sheet1.UsedRange
lr1 = .Rows.Count
lc1 = .Columns.Count
End With
With Sheet3.UsedRange
lr2 = .Rows.Count
lc2 = .Columns.Count
End With
maxR = lr1
maxC = lc1
If maxR < lr2 Then maxR = lr2
If maxC < lc2 Then maxC = lc2
DiffCount = 0
For c = 1 To maxC
For i = 2 To lr1
diffB = True
Application.StatusBar = "Comparing cells " & Format(i/maxR, "0 %") & "..."
For r = 2 To lr2
cf1 = ""
cf2 = ""
On Error Resume Next
cf1 = Sheet1.Cells(i, c).FormulaLocal
cf2 = Sheet3.Cells(r, c).FormulaLocal
On Error GoTo 0
If cf1 = cf2 Then
diffB = False
Sheet1.Cells(i, c).Interior.ColorIndex = 19
Sheet1.Cells(i, c).Select
Selection.Font.Bold = True
Exit For
End If
Next r
If diffB Then
DiffCount = DiffCount + 1
Sheet1.Cells(i, c).Interior.ColorIndex = 0
Sheet1.Cells(i, c).Select
Selection.Font.Bold = False
End If
Next i
Next c3
Application.StatusBar = "Formatting the report..."
'Columns("A:IV").ColumnWidth = 10
m = maxR - DiffCount - 1
Application.StatusBar = False
Application.ScreenUpdating = True
MsgBox m & " cells contain same values!", vbInformation, _
"Compare " & Sheet1.Name & " with " & Sheet3.Name
然而,這比列,我不知道如何限制比較列E-XFD在Sheet1列A-XFD Sheet 2中。
此工作簿中還有幾張紙,但我只想比較sheet1和sheet2。
非常感謝,如果你們能幫助我:)
謝謝!
歡迎來到SO!鏈接到你看過的帖子並告訴我們爲什麼你發現它們不令人滿意 - 這有助於我們改進答案。我很困惑,你想將第2頁的X列與第1頁的X-5列進行比較?你這樣做的算法是什麼?您是將Sheet1中的多個列與Sheet2中的同一列進行比較? Sheet2中有一些列會跳過你忘記提及的內容嗎? – FreeMan
而不是'For c = 1 To maxC',您需要定義第一列(而不是1)。 – user3819867
我認爲[我的回答](http://stackoverflow.com/a/29651846/4519059)可以幫助;)。 –