2013-01-07 62 views
2

我想顏色突出顯示彼此不同的單元格;在這種情況下colA和colB。該功能適用​​於我所需要的功能,但看起來反覆,醜陋,效率低下。我不熟悉VBA編碼;有沒有更優雅的書寫這個功能的方法?VBA宏來比較兩列和顏色突出顯示單元格區別

編輯 我試圖讓這個功能做的是:1。 亮點細胞可樂具有不同或不COLB 2.亮點細胞COLB具有不同或不可樂

Sub compare_cols() 

    Dim myRng As Range 
    Dim lastCell As Long 

    'Get the last row 
    Dim lastRow As Integer 
    lastRow = ActiveSheet.UsedRange.Rows.Count 

    'Debug.Print "Last Row is " & lastRow 

    Dim c As Range 
    Dim d As Range 

    Application.ScreenUpdating = False 

    For Each c In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells 
     For Each d In Worksheets("Sheet1").Range("B2:B" & lastRow).Cells 
      c.Interior.Color = vbRed 
      If (InStr(1, d, c, 1) > 0) Then 
       c.Interior.Color = vbWhite 
       Exit For 
      End If 
     Next 
    Next 

    For Each c In Worksheets("Sheet1").Range("B2:B" & lastRow).Cells 
     For Each d In Worksheets("Sheet1").Range("A2:A" & lastRow).Cells 
      c.Interior.Color = vbRed 
      If (InStr(1, d, c, 1) > 0) Then 
       c.Interior.Color = vbWhite 
       Exit For 
      End If 
     Next 
    Next 

Application.ScreenUpdating = True 

End Sub 
+4

如何徹底擺脫VBA,並使用XL強大的'Conditional Formatting'功能?此外,也許這更適合[代碼評論](http://codereview.stackexchange.com/) –

+0

@ScottHoltzman所有版本都可以使用該功能嗎? – Kermit

+0

@njk - >好問題。雖然如此,但07/10賽季的功能比03更強勁,但我不確定07/10賽季的差距,儘管如此,我的頭腦還是不錯。 –

回答

3

啊是的,這是蛋糕我整天都在做。其實你的代碼看起來非常像我這樣做。雖然,我選擇使用整數循環,而不是使用「For Each」方法。我可以在代碼中看到的唯一潛在問題是ActiveSheet可能並不總是「Sheet1」,並且InStr已知會給出有關vbTextCompare參數的一些問題。使用給定的代碼,我將它更改爲以下:

Sub compare_cols() 

    'Get the last row 
    Dim Report As Worksheet 
    Dim i As Integer, j As Integer 
    Dim lastRow As Integer 

    Set Report = Excel.Worksheets("Sheet1") 'You could also use Excel.ActiveSheet _ 
              if you always want this to run on the current sheet. 

    lastRow = Report.UsedRange.Rows.Count 

    Application.ScreenUpdating = False 

    For i = 2 To lastRow 
     For j = 2 To lastRow 
      If Report.Cells(i, 1).Value <> "" Then 'This will omit blank cells at the end (in the event that the column lengths are not equal. 
       If InStr(1, Report.Cells(j, 2).Value, Report.Cells(i, 1).Value, vbTextCompare) > 0 Then 
        'You may notice in the above instr statement, I have used vbTextCompare instead of its numerical value, _ 
        I find this much more reliable. 
        Report.Cells(i, 1).Interior.Color = RGB(255, 255, 255) 'White background 
        Report.Cells(i, 1).Font.Color = RGB(0, 0, 0) 'Black font color 
        Exit For 
       Else 
        Report.Cells(i, 1).Interior.Color = RGB(156, 0, 6) 'Dark red background 
        Report.Cells(i, 1).Font.Color = RGB(255, 199, 206) 'Light red font color 
       End If 
      End If 
     Next j 
    Next i 

    'Now I use the same code for the second column, and just switch the column numbers. 
    For i = 2 To lastRow 
     For j = 2 To lastRow 
      If Report.Cells(i, 2).Value <> "" Then 
       If InStr(1, Report.Cells(j, 1).Value, Report.Cells(i, 2).Value, vbTextCompare) > 0 Then 
        Report.Cells(i, 2).Interior.Color = RGB(255, 255, 255) 'White background 
        Report.Cells(i, 2).Font.Color = RGB(0, 0, 0) 'Black font color 
        Exit For 
       Else 
        Report.Cells(i, 2).Interior.Color = RGB(156, 0, 6) 'Dark red background 
        Report.Cells(i, 2).Font.Color = RGB(255, 199, 206) 'Light red font color 
       End If 
      End If 
     Next j 
    Next i 

Application.ScreenUpdating = True 

End Sub 

的東西,確實是不同的:

  1. 我使用上述我的整數方法(而不是在「每個」法)。
  2. 我將工作表定義爲對象變量。
  3. 我使用vbTextCompare而不是InStr函數中的數值。
  4. 我添加了一個if語句來省略空白單元格。提示:即使只有一個 在表列超長(例如,小區D5000被意外 格式),那麼所有列的usedrange被認爲是5000
  5. 我使用的RGB代碼的顏色(它只是更容易因爲我 有一張備忘單固定在我旁邊的牆上,在這個小屋 哈哈)。

那麼總結一下。祝你的項目好運!