在coulmn中查找重复项,并用备用颜色突出显示行背景颜色

初始内容

处理的内容

  1. 当“A”列中的值相同时,我想要用相同的颜色突出显示行(文本在列“A”到“G”)的单元格颜色,并且想要为所有行迭代相同的操作并应用替代颜色。
  2. 当“F”列中有文件“EMEA服务器上的文件”时,也希望在2个单元格(“F”和“G”列)中将字体颜色更改为红色。

此macros在2种颜色之间交替,当A列中的值发生变化时,该macros会改变。

 Sub Highlighting() Dim rw As Long Dim lastrw As Long ' Define 2 different highlighting colours by their RGB values Dim col1 As Long Dim col2 As Long col1 = RGB(255, 230, 180) col2 = RGB(180, 230, 255) ' "Current" colour for highlighting Dim col As Long col = col1 With ThisWorkbook.ActiveSheet ' Get last row by last non-empty cell in column A lastrw = .Cells(.Rows.Count, 1).End(xlUp).Row ' Cycle through column A For rw = 1 To lastrw ' Highlight row with current colour .Range("A" & rw & ":G" & rw).Interior.Color = col ' If column A value in next row is different, change colour If .Cells(rw + 1, 1).Value <> .Cells(rw, 1) Then ' Set to whichever colour it is not If col = col1 Then col = col2 Else col = col1 End If End If Next rw End With End Sub 

之前:

之前

后:

后

在某些情况下,您应该可以将字体颜色更改为红色,方法是在主For循环中插入自己的条件If .Range("F" & rw).Value = "Files ... "等。