VBAmacros比较两列和颜色突出显示单元格差异

我想要突出显示彼此不同的单元格; 在这种情况下colA和colB。 这个函数适用于我所需要的,但看起来是重复的,丑陋的和低效的。 我不熟悉VBA编码; 有没有更优雅的方式来写这个function?

编辑我试图让这个function要做的是:1.突出ColA中不同或不在ColB中的细胞2.突出显示ColB中不同或不在ColA中的细胞

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 

啊,这是蛋糕,我整天都在做。 其实你的代码看起来非常像我这样做。 虽然,我select循环使用整数,而不是使用“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. 我用上面描述的整数方法(而不是'for each'方法)。
  2. 我将工作表定义为一个对象variables。
  3. 我使用vbTextCompare而不是InStr函数中的数值。
  4. 我添加了一个if语句来省略空白单元格。 提示:即使工作表中只有一列超长(例如,单元格D5000被意外格式化),那么所有列的已用区域也被认为是5000。
  5. 我使用rgb代码的颜色(这对我来说更容易,因为我有一个备忘单固定在我旁边的这个隔间哈哈)。

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