简体   繁体   中英

Formula works in conditional formatting but not in VBA

The formula in the code below works when I use it in conditional formatting but not in VBA :

Sub setCondFormat()
    Range("P69").Select
    With Range("P69:P10000")
        .FormatConditions.Add Type:=xlExpression, Formula1:= _
          "=AND($P69>$S69;$B69<>$B70:$B241;$I69<>"")"
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            With .Interior
                .PatternColorIndex = xlAutomatic
                .Color = -6052865
                .TintAndShade = 0
            End With
        End With
    End With
End Sub

I already tried changing ; to , and it didn't work. I also deleted $ but that didn't work either.

When I used only this formula it worked: =$P69>$S69

When I deleted = it worked but then I have this formula in conditional formatting: ="AND($P69>$S69;$B69<>$B70:$B241;$I69<>"")" and of course that doesn't work

I have English version.

Do you please know what the problem is?

The problem seems to be with the quotation marks in the last part of the formula: $I69<>"" . You have to escape them by doubling:

Formula1:= "=AND($P69>$S69,$B69<>$B70:$B241,$I69<>"""")"

I would also suggest that as a beginner, you could try using the macro recorder to see the code behind your UI actions, like in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM