vba excel 条件格式 - 无效的过程调用或参数

vba excel conditional formatting - invalid Procedure call or argument

我有一个 Excel sheet,我想在其中添加一个 VBA 的条件公式。当我尝试这样做时,Excel 抛出“无效的过程调用或参数”,我找不到原因。

问题出在这一行:

Set cf = shG.Range("E" & (i - 3) & ":AI" & (i - 2)).FormatConditions.Add(type:=xlExpression, Formula1:="=OR(ISNUMBER(SEARCH(""holiday"",E$" & i & ")),ISNUMBER(SEARCH(""l4"",E$" & i & ")))")

它应该做什么?

It should change font color to white for 1st and 2nd rows in case if 4th row will contain "holiday" or "l4"

完整子:

Sub AddCondForm(shG As Worksheet)
    Dim r As Range
    Dim cf
    Set r = shRoles.Cells(2, 1)
    Do
        For i = 8 To 204 Step 4
            If Not IsEmpty(shRoles.Cells(r.row, 2)) Then
                Set cf = shG.Range("E" & i & ":AI" & i).FormatConditions.Add(type:=xlTextString, String:=r.Value, _
                TextOperator:=xlBeginsWith)
            Else
                Set cf = shG.Range("E" & i & ":AI" & i).FormatConditions.Add(type:=xlCellValue, Operator:=xlEqual, _
                Formula1:=r.Value)
            End If
            cf.Interior.Color = r.Next.Next.Next.Interior.Color
            cf.Font.Color = r.Next.Next.Next.Font.Color
            cf.SetFirstPriority

'HERE IS PROBLEM

            Set cf = shG.Range("E" & (i - 3) & ":AI" & (i - 2)).FormatConditions.Add(type:=xlExpression, Formula1:="=OR(ISNUMBER(SEARCH(""holiday"",E$" & i & ")),ISNUMBER(SEARCH(""l4"",E$" & i & ")))")

'END OF PROBLEM
                cf.Font.ColorIndex = 2 
                'Coloring C Column 
                Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1")
                cf.Interior.ThemeColor = xlThemeColorAccent6
                Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=3/4")
                cf.Interior.ThemeColor = xlThemeColorAccent6
                cf.Interior.TintAndShade = 0.2
                Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1/2")
                cf.Interior.ThemeColor = xlThemeColorAccent6
                cf.Interior.TintAndShade = 0.4
                Set cf = shG.Range("C" & (i - 3) & ":C" & i).FormatConditions.Add(type:=xlExpression, Formula1:="=$A$" & i - 3 & "=1/4")
                cf.Interior.ThemeColor = xlThemeColorAccent6
                cf.Interior.TintAndShade = 0.6
            Next
            Set r = shRoles.Cells(r.row + 1, 1)
        Loop Until IsEmpty(r.Value)
    End Sub

PS。我检查了公式本身,它似乎工作正常。其他线路也运行良好。只有这个,我不知道为什么:'(

如果您不是英语用户,这对您可能很重要。

FormatCondition.Add 的情况下,您需要添加与手动输入完全相同的公式。因此,如果您的语言中的分隔符不是逗号,例如在波兰(我们有分号),您将不得不使用该分隔符并将您的公式翻译成该语言。

在我的例子中,这意味着我必须将公式更改为

=LUB(CZY.LICZBA(SZUKAJ.TEKST(""holiday"";E$" & i & "));CZY.LICZBA(SZUKAJ.TEKST(""l4"";E$" & i & ")))"