【问题标题】:Variables in Conditional Formatting Formula1条件格式公式1中的变量
【发布时间】:2014-09-05 14:10:14
【问题描述】:

我正在尝试在 FormatCondition Formula1 属性中使用变量。变量将是单元格引用。但是,我无法正确使用语法。我在下面的代码中遇到问题的两个位是:"=(C$3:J$10=""CM"")""=($C3:$J10=""RM"")"

这样做的目的是在某个单元格中用 CM 突出显示一列,并在某个单元格中用 RM 突出显示一行。列数和行数会增加和减少,因此使用变量。

或者,如果这不是正确的方式或最佳方式,我们将不胜感激。

代码是:

Private Sub Workbook_Open()
Application.ScreenUpdating = False
'Rows
Dim iRowA As Integer, iRowB As Integer, iRowC As Integer
Dim iRowDataStart As Integer, iRowLast As Integer
'Columns
Dim iColX As Integer, iColY As Integer, iColZ As Integer
Dim iColDataStart As Integer, iColLast As Integer
'Ranges
Dim rAll As Range
Dim rRowB As Range, rColY As Range
Dim rRowMark As Range, rColMark As Range
'String
Dim sString As String
'Assign values, normally these would be variable values, not assigned
iRowA = 1: iRowB = 2: iRowC = 3
iRowDataStart = 4: iRowLast = 10
iColX = 1: iColY = 2: iColZ = 3
iColDataStart = 4: iColLast = 10
'Set ranges
Set rAll = Range(Cells(iRowA, iColX), Cells(iRowLast, iColLast))
Set rRowB = Range(Cells(iRowB, iColZ), Cells(iRowLast, iColLast))
Set rColY = Range(Cells(iRowC, iColY), Cells(iRowLast, iColLast))
Set rRowMark = Range(Cells(iRowC, iColZ), Cells(iRowLast, iColLast))
Set rColMark = Range(Cells(iRowC, iColZ), Cells(iRowLast, iColLast))
'Delete all CF currently in the worksheet
With rAll
    .FormatConditions.Delete
End With
'Format column with Column Mark
sString = "=(C$3:J$10=""CM"")"
With rRowB
    .FormatConditions.Add _
        Type:=xlExpression, _
        Formula1:=sString
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1)
        .Interior.Color = RGB(196, 189, 151)
        .StopIfTrue = False
    End With
End With
'Format row with Row Mark
sString = "=($C3:$J10=""RM"")"
With rColY
    .FormatConditions.Add _
        Type:=xlExpression, _
        Formula1:=sString
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1)
        .Font.ColorIndex = 2
        .Interior.Color = RGB(127, 127, 127)
        .StopIfTrue = False
    End With
End With
Range("A1").Select
Application.StatusBar = False
Application.CutCopyMode = False
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您只需要通过获取数据的最后一行和最后一列来动态设置您的范围,您可以在这里找到许多示例like this one。比如:

    Dim r As Range
    Dim lr As Long, lc As Long
    Dim formula As String
    
    With Sheet1 '~~> change to your actual sheet
        lr = .Range("C" & .Rows.Count).End(xlUp).Row '~~> based on C, adjust to suit
        lc = .Cells(3, .Columns.Count).End(xlToLeft).Column '~~> based on row 3
        Set r = .Range(.Cells(3, 3), .Cells(lr, lc))
        formula = "=(" & r.Address & "=""CM"")"
        '~~> formatting code here
    End With
    

    或者你可以试试我在这里发布的关于Conditional Formatting 的内容,当然可以自动化,因为我发布了HEREHERE。比如:

    formula = "=C3=""CM"""
    [C3].FormatConditions.Add xlExpression, , formula
    With [C3].FormatConditions(1)
        .Interior.Color = RGB(196, 189, 151)
        .ModifyAppliesToRange r
    End With
    

    HTH。

    【讨论】:

    • 这对获取最后一个变量行和列以突出显示整个数据区域非常有帮助。但我想要的是把 CM 放在一个变量列中,或者把 RM 放在一个变量行中,并在数据区域中突出显示该列或行。这可能吗?
    猜你喜欢
    • 1970-01-01
    • 2014-12-27
    • 2014-12-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多