Excel:行标题的条件格式而不是行号

时间:2014-05-29 20:25:31

标签: excel vba formula

我需要在特定行的150个标签中突出显示1到30之间的值。此行在每个选项卡之间的第6行,第7行,第8行和第9行之间波动,因此我尝试通过名为TOTAL的行标题执行此操作。这可能吗?

1 个答案:

答案 0 :(得分:0)

OP在评论中发布他现在只想将条件格式应用于所有工作表中的第6行,而不是可能包含单词TOTAL的各行。为此,请在Excel 2007中

Apply the desired conditional formatting to Row 6 on a single sheet.
Select the entire Row 
Select the Format Painter 
Navigate to the next sheet
Select all of the sheets you wish to CF Row 6
Select Row 6 (click on the 6 with the Format Painter)

虽然您无法在Excel 2007中同时CF多张图纸,但您可以CF单张图纸,然后将该格式复制到同时显示多张图纸

编辑:我看到您录制了一个宏来生成条件格式并将其粘贴到您的一条评论中。

下一步是修改它以在其中一行6:8中找到单词Total,并仅将格式应用于该行。

以下是对您发布的内容的修改,应该为每个工作表执行此操作。我还清理了宏录制器生成的代码。请注意,宏假定Row标头始终位于第一列(A列)中。它循环遍历每个工作表并将格式应用于该范围内的相关行。

此外,这可能是也可能不需要,宏从这些行中的其他单元格中删除条件格式。

尝试一下:

Option Explicit
Sub Macro1()
    Dim R As Range, C As Range
    Dim WS As Worksheet

For Each WS In ThisWorkbook.Worksheets
    Set R = WS.Rows("6:8")
        R.FormatConditions.Delete  'get rid of old formatconditions
    With R.Columns(1)
        Set C = .Find(what:="TOTAL", LookIn:=xlValues, _
            lookat:=xlWhole, MatchCase:=False)
    End With
    If Not C Is Nothing Then
        With C.EntireRow
            .FormatConditions.Add _
                Type:=xlCellValue, _
                Operator:=xlBetween, _
                Formula1:="=1.0000001", _
                Formula2:="=30"
            With .FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic
                .Color = 13551615
                .TintAndShade = 0
            End With
            .FormatConditions(1).StopIfTrue = False
        End With
    End If
Next WS
End Sub

修改 OP现在需要0到30的范围,但要排除空单元格。因此,我们将CF改为公式(表达式),该公式还将测试单元格中是否有数字:

Option Explicit
Sub Macro1()
    Dim R As Range, C As Range
    Dim WS As Worksheet

For Each WS In ThisWorkbook.Worksheets
    Set R = WS.Rows("6:8")
        R.FormatConditions.Delete  'get rid of old formatconditions
    With R.Columns(1)
        Set C = .Find(what:="TOTAL", LookIn:=xlValues, _
            lookat:=xlWhole, MatchCase:=False)
    End With
    If Not C Is Nothing Then
        With C.EntireRow
            .FormatConditions.Add _
                Type:=xlExpression, _
                Formula1:="=and(isnumber(" & C.Address(True, False) & "), " & _
                    C.Address(True, False) & ">=0, " & _
                    C.Address(True, False) & "<=30)"
            With .FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic
                .Color = 13551615
                .TintAndShade = 0
            End With
        End With
    End If
Next WS
End Sub