Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-条件格式设置一行中的多个规则_Excel_Vba_Search_Conditional Formatting - Fatal编程技术网

Excel VBA-条件格式设置一行中的多个规则

Excel VBA-条件格式设置一行中的多个规则,excel,vba,search,conditional-formatting,Excel,Vba,Search,Conditional Formatting,我想通过VBA应用条件格式,如果列E包含例如1ST,那么我想对它旁边的28个单元格使用多个条件格式规则 这一刻我用 Sub SetFormulasFormat() With ActiveSheet For Each cl In Application.Intersect(.Columns("E"), .UsedRange) ' found upper row of the data in table If UCase(cl.Text)

我想通过VBA应用条件格式,如果列E包含例如1ST,那么我想对它旁边的28个单元格使用多个条件格式规则

这一刻我用

Sub SetFormulasFormat()
With ActiveSheet
    For Each cl In Application.Intersect(.Columns("E"), .UsedRange)
        ' found upper row of the data in table
        If UCase(cl.Text) = "1ST" Then
                    cl.Resize(, 1).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
                     Formula1:="=1"
                    cl.Resize(, 1).FormatConditions(1).Interior.Color = vbRed
                    
                    cl.Resize(, 2).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
                     Formula1:="=3"
                    cl.Resize(, 2).FormatConditions(2).Interior.Color = vbRed
                

            End If
        
    Next cl
End With
端接头

但我不适用第二条规则

我的excel示例

有人能帮我吗?

试试:

Sub SetFormulasFormat()
Application.ScreenUpdating = False
Dim cl As Range
With ActiveSheet
    For Each cl In Application.Intersect(.Columns("E"), .UsedRange)
        ' found upper row of the data in table
        If UCase(cl.Value) = "1ST" Then
            .Range("F" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=1" 'apply CF rule to 1 single cell in same row
            .Range("F" & cl.Row).FormatConditions(1).Interior.Color = vbRed
            
            .Range("G" & cl.Row & ",H" & cl.Row).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=3" 'apply CF rule to 2 different cells in same row (Separate each cell with , like G2,H2...)
            .Range("G" & cl.Row & ",H" & cl.Row).FormatConditions(1).Interior.Color = vbRed
        End If
        
    Next cl
End With
Application.ScreenUpdating = True
End Sub
这将在您指定的每个范围内应用一个单个CF规则。我键入了在单个单元格或两个单元格中创建CF规则的示例,但您可以根据需要调整它。

Variable Conditional Formatting
  • ”*
    表示您必须更改其他值的代码,而不是
    1ST
    ,例如
    CUCA
代码

Option Explicit

Sub SetFormulasFormat()
    Const hRows As Long = 1
    Const cOffset As Long = 1
    Dim cl As Range
    
    Dim Offsets As Variant
    Offsets = Array(1, 2, 3)
    Dim firstValues As Variant
    firstValues = Array(1, 3, 5) '*
'    Dim cucaValues As Variant
'    cucaValues = Array(1, 3, 5) '*
    
    Dim cLower As Long: cLower = LBound(Offsets)
    Dim cUpper As Long: cUpper = UBound(Offsets)
    
    Dim fcols As Long: fcols = cUpper - cLower + 1
    With ActiveSheet
        Dim crg As Range
        With Intersect(.Columns("E"), .UsedRange)
            Set crg = .Resize(.Rows.Count - hRows).Offset(hRows)
        End With
    End With
    With crg
        .Resize(, fcols).Offset(, cOffset).FormatConditions.Delete
        Dim n As Long
        For Each cl In .Cells
            Select Case True
            Case UCase(cl.Value) = "1ST" '*
                For n = cLower To cUpper
                    With cl.Offset(, Offsets(n))
                        .FormatConditions.Add Type:=xlCellValue, _
                            Operator:=xlLess, Formula1:="=" & firstValues(n) '*
                        With .FormatConditions(1)
                            .Font.Color = vbWhite
                            .Interior.Color = vbRed
                        End With
                    End With
                Next n
'            Case UCase(cl.Value) = "CUCA" '*
'                For n = cLower To cUpper
'                    With cl.Offset(, Offsets(n))
'                        .FormatConditions.Add Type:=xlCellValue, _
'                            Operator:=xlLess, Formula1:="=" & cucaValues(n) '*
'                        With .FormatConditions(1)
'                            .Font.Color = vbWhite
'                            .Interior.Color = vbRed
'                        End With
'                    End With
'                Next n
            End Select
        Next cl
    End With
End Sub

您的代码创建了一个CF规则,该规则只检查单元格值是否为1(或3)。您是否尝试根据列E=1ST中的if值为整行着色?如果文本为1ST,我需要它检查列F是否小于1,然后它需要检查列G和H是否小于3,如果您知道我的意思,则需要检查其他列的其他数字。它只需要突出显示需要检查的cel,就像一个符咒,但我想添加另一个文本,我得到运行时错误应用程序对象未找到。请参阅我在此初始单元格下的帖子,必须是一个字母,如
“H”
,而不是
,H
。只有第一个。此外,如果单元格相邻,您可以只使用第一列和最后一列,这样您就可以执行
范围(“H”&cl.row&“Y”)“&cl.row
这将是H和YHad之间的所有单元格刚刚找到它。谢谢您的帮助!可能还有另一个问题。我在一张工作表中有7天,我想应用我现在只有周一到周四的内容,然后在其他几天我需要其他规则。我如何做到这一点?您需要为此打开一个新问题。检查[当有人回答我的问题时,我该怎么办?()