VBA基于单元格内容的Hightlight单元格

我正在努力让这个macros工作。 我希望它对于每一行,当“晚”进入列C时,突出显示单元格2左侧的空格和单元格的范围3空格右侧通过43.所以例子是C4包含“晚”,突出显示A4和F4:AW4。 “Hold”这个词也是一个不同的颜色。

Private Sub Highlight_Condition(ByVal Target As Range) Dim lastRow As Long Dim cell As Range Dim i As Long With ActiveSheet lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row Application.EnableEvents = False For i = lastRow To 1 Step -1 If .Range("C" & i).Value = "LATE" Then Debug.Print "Checking Row: " & i .Range("A" & i).Interior.ColorIndex = 39 .Range("F" & i & ":AW" & i).Interior.ColorIndex = 39 ElseIf .Range("C" & i).Value = "HOLD" Then .Range("A" & i).Interior.ColorIndex = 43 .Range("F" & i & ":AW" & i).Interior.ColorIndex = 43 Else .Range("A" & i & ":AW" & i).ClearContents .Range("F" & i & ":AW" & i).ClearContents End If Next i Application.EnableEvents = True End With End Sub 

这应该适合你…

 Private Sub Highlight_Condition(ByVal Target As Range) Dim lastRow As Long Dim cell As Range Dim i As Long With ActiveSheet lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row Application.EnableEvents = False For i = lastRow To 1 Step -1 If .Range("C" & i).Value = "LATE" Then Debug.Print "Checking Row: " & i .Range("A" & i).Interior.ColorIndex = 39 .Range("F" & i & ":AW" & i).Interior.ColorIndex = 39 ElseIf .Range("C" & i).Value = "HOLD" Then .Range("A" & i).Interior.ColorIndex = 43 .Range("F" & i & ":AW" & i).Interior.ColorIndex = 43 Else .Range("A" & i & ":AW" & i).ClearContents .Range("F" & i & ":AW" & i).ClearContents End If Next i Application.EnableEvents = True End With End Sub 

经过testing,似乎为我工作正常:)

C4 包含 “迟到”… (强调我的)

这似乎表明, 晚期可能是一个更长的string的一部分。 我将为此编码。

条件格式化规则是实现单元格高亮显示的一种快速方法,只要C列中的值发生更改,而无需重新运行子过程(除非在lastRow下面添加更多值),就会立即进行响应。

 Option Explicit Sub Macro1() Const TEST_COLUMN As String = "D" Dim lastRow As Long, sSheetName As String sSheetName = ActiveSheet.Name With Worksheets(sSheetName) lastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row With .Range("A4:A" & lastRow & ", F4:AW" & lastRow) .FormatConditions.Delete .FormatConditions.Add Type:=xlExpression, Formula1:="=isnumber(search(""late"", $c4))" .FormatConditions(.FormatConditions.Count).Interior.ColorIndex = 39 .FormatConditions.Add Type:=xlExpression, Formula1:="=isnumber(search(""hold"", $c4))" .FormatConditions(.FormatConditions.Count).Interior.ColorIndex = 43 End With End With End Sub 

大! 我想在工作表中运行而不是模块。 所以我添加了一些额外的行和BYVAL目标作为范围每次在范围内进行更改,但它似乎并没有工作。 我错过了什么?

 Private Sub Highlight_Condition(ByVal Target As Range) Dim LastRow As Long Dim cell As Range Dim i As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row Application.EnableEvents = False For i = LastRow To 1 Step -1 If .Range("C" & i).Value = "LATE" Then Debug.Print "Checking Row: " & i .Range("A" & i).Interior.ColorIndex = 39 .Range("F" & i & ":AW" & i).Interior.ColorIndex = 39 ElseIf .Range("C" & i).Value = "HOLD" Then .Range("A" & i).Interior.ColorIndex = 43 .Range("F" & i & ":AW" & i).Interior.ColorIndex = 43 Else .Range("A" & i).EntireRow.Interior.ColorIndex = xlNone End If Next i Application.EnableEvents = True End With End Sub