使用Excel VBA基于单元格内容突出显示单元格

时间:2017-12-07 18:01:27

标签: excel vba excel-vba

这适用于Microsoft Excel VBA宏。它应该做什么,对于每一行,当"晚期"进入C列,突出显示左边的单元格2个空格和单元格范围3个空格到右边到43个。例如C4包含" Late&#34 ;,高亮显示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

3 个答案:

答案 0 :(得分:1)

这对你有用......

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

经过测试,似乎对我很好。)

答案 1 :(得分:0)

  

... C4 包含"晚期" ...... (强调我的)

这似乎表明 Late 可能是较长字符串的一部分。我会为此编写代码。

条件格式设置规则是一种快速实现单元格突出显示的方法,只要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

答案 2 :(得分:-1)

大!我想在工作表中运行它而不是模块。所以我添加了一些额外的线和ByVal Target As Range来每次在范围内进行更改时触发,但它似乎不起作用。我错过了什么吗?

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