我试图在Excel工作表上工作,同时在应用滤镜时会返回特定的颜色。 例如,当我在活动列上应用过滤器时,它应该返回绿色。并且当我移除过滤器时,绿色应该消失。
我一直在使用下面的VBA代码,但由于先前应用的过滤器列仍显示为绿色,这会造成混乱,因此无法进行工作。
Sub ColorFilterColumn()
Dim flt As Filter
Dim iCol As Integer
Dim lRow As Long
Dim rTemp As Range
Dim bFullCol As Boolean
' Set as True if you want entire column shaded
bFullCol = True
If ActiveSheet.AutoFilterMode Then
iCol = ActiveSheet.AutoFilter.Range.Column
lRow = ActiveSheet.AutoFilter.Range.Row
Application.EnableEvents = False
For Each flt In ActiveSheet.AutoFilter.Filters
If bFullCol Then
Set rTemp = Cells(lRow, iCol).EntireColumn
Else
Set rTemp = Cells(lRow, iCol)
End If
If flt.On Then
rTemp.Interior.Color = vbYellow
Else
rTemp.Interior.ColorIndex = xlColorIndexNone
End If
Set rTemp = Nothing
iCol = iCol + 1
Next flt
Application.EnableEvents = True
End If
End Sub
有人可以帮我吗