2013-01-07 58 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代码的颜色(它只是更容易因为我 有一张备忘单固定在我旁边的墙上,在这个小屋 哈哈)。

那么总结一下。祝你的项目好运!

相关问题