【问题标题】:VBA - conditional formatting mutiple rules in one rowVBA - 在一行中有条件地格式化多个规则
【发布时间】:2021-03-08 13:24:53
【问题描述】:

如果列 E 包含例如 1ST,我想通过 VBA 应用条件格式,那么我想对它旁边的 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示例

有人可以帮我吗?

【问题讨论】:

  • 您的代码创建了一个 CF 规则,它只检查单元格值是否为 1(或 3)。您是否尝试根据列 E = 1ST 中的值为整行着色?
  • 如果文本是 1ST,我需要它来检查 F 列是否小于 1,然后它需要检查 G 和 H 列是否小于 3,如果你需要检查其他列的其他数字明白我的意思了吗。它只需要突出显示它需要检查的 cel

标签: excel vba search conditional-formatting


【解决方案1】:

试试:

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 规则。我输入了在单个单元格或 2 个单元格中创建 CF 规则的示例,但您可以根据需要进行调整。

【讨论】:

  • 像一个魅力,但我想添加另一个文本,我得到运行时错误 application-object not found。看我在这个下的帖子
  • 初始单元格必须只是一个字母,如"H",而不是",H。只是第一个。此外,如果单元格是相邻的,您可以只使用第一列和最后一列,所以您可以使用Range("H" & cl.row & "Y" & cl.row,这将是 H 和 Y 之间的 所有 单元格
  • 刚刚找到它。谢谢您的帮助!也许是另一个问题。我在一张纸上有 7 天,我想应用我现在只在周一到周四拥有的东西,然后在其他日子里我需要其他规则。我该怎么做?
  • 您需要为此打开一个新问题。检查[当有人回答我的问题时我该怎么办? ](stackoverflow.com/help/someone-answers)
【解决方案2】:

变量条件格式

  • '* 表示您必须在哪里更改其他值的代码,而不是 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 2017-03-05
    • 2014-03-09
    • 2019-11-09
    相关资源
    最近更新 更多