2015-04-17 82 views
1

評論說,也有類似的問題之前,我已經嘗試過他們,但他們沒有工作不幸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。

非常感謝,如果你們能幫助我:)

謝謝!

+0

歡迎來到SO!鏈接到你看過的帖子並告訴我們爲什麼你發現它們不令人滿意 - 這有助於我們改進答案。我很困惑,你想將第2頁的X列與第1頁的X-5列進行比較?你這樣做的算法是什麼?您是將Sheet1中的多個列與Sheet2中的同一列進行比較? Sheet2中有一些列會跳過你忘記提及的內容嗎? – FreeMan

+0

而不是'For c = 1 To maxC',您需要定義第一列(而不是1)。 – user3819867

+0

我認爲[我的回答](http://stackoverflow.com/a/29651846/4519059)可以幫助;)。 –

回答

0
Dim lrOne As Integer 
Dim lcOne As Integer 
Dim lrTwo As Integer 
Dim lcTwo As Integer 
Dim cellA As Variant 
Dim cellB As Variant 
Dim cellCnt As Integer 
Dim lookupRange As Range 
Dim lookinRange As Range 

lrOne = Sheet1.Cells(Rows.Count, 5).End(xlUp).Row 
lrTwo = Sheet3.Cells(Rows.Count, 1).End(xlUp).Row 
lcOne = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column 
lcTwo = Sheet3.Cells(1, Columns.Count).End(xlToLeft).Column 

Set lookupRange = Sheet1.Range(Cells(1,5), Cells(lrOne, lcOne)) 
Set lookinRange = Sheet3.Range(Cells(1,1), Cells(lrTwo, lcTwo)) 

For Each cellA In lookupRange 
    For Each cellB in lookinRange 
     If cellA.Value = cellB.Value And cellA.Value <> "" Then 
      cellB.Interior.ColorIndex = 3 
      cellCnt = cellCnt + 1 
     End If 
    Next cellB 
Next cellA 
+0

謝謝@Mortiz Schmitz v。Hülst,我試過你的代碼並在其中添加了一行(更改顏色) 'cell.Interior.ColorIndex = 3' 但我得到一個錯誤404,缺少對象 – Nash

+0

因爲你添加了' cell'而不是'cellB'或'cellA'。 –

+0

'子Macro3() 昏暗LR作爲整數 昏暗LC作爲整數 昏暗CELLA作爲變 昏暗cellB作爲變 昏暗cellCnt只要 昏暗lookupRange作爲範圍 昏暗lookinRange作爲範圍 集lookupRange = Sheet3.Range ( 「E:XFD」) 集lookinRange = Sheet1.Range( 「A:XFD」) 對於每個cellB在lookupRange 對於每個CELLA在lookinRange 如果cellB.Value <> cellA.Value然後 cellB.Interior。ColorIndex = 3「改變顏色 cellCnt = cellCnt + 1 結束如果 接着CELLA 接着cellB MSGBOX‘的差異突出了’ 結束Sub' 這裏是編輯的代碼,但是現在所有的單元被高亮顯示 – Nash