Help with a conditional format (VBA)

Hi all,

I'm attempting to run the following code, which loops through various ranges on a worksheet installing conditional formats at these ranges using different formulas, but getting a Run Time '1004' error. The  line that fails is:

Selection.FormatConditions.Add Type:=xlExpression, Formula1:=Formula(a) (n.b. its the second line of code inside the loop)

Obviously Excel is not digging the way I'm using the variable 'Formula(a)' at the end of the line. Any suggestions on how to finish this line, or use the variable?
cheers!


Sub conFors()
    Dim a As Integer
    Dim RangeNum(3) As String
    Dim Formula(3) As String
    
    RangeNum(0) = "B2:B3"
    RangeNum(1) = "B5"
    RangeNum(2) = "B11:B50"
    RangeNum(3) = "F11:F50"
    
    Formula(0) = "=ISBLANK(B2)=TRUE"
    Formula(1) = "=ISBLANK(B5)=TRUE"
    Formula(2) = "=IF((AND(ISBLANK(B11)=TRUE,ISBLANK(A11)=FALSE)),TRUE)"
    Formula(3) = "=IF((OR(ISBLANK(A11)=FALSE,ISBLANK(B11)=FALSE))*((AND(ISBLANK(F11)=TRUE))),TRUE,FALSE)"

    For a = 0 To 3
        Range(RangeNum(a)).Select
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:=Formula(a)
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .Pattern = xlPatternLinearGradient
            .Gradient.Degree = 90
            .Gradient.ColorStops.Clear
        End With
        With Selection.FormatConditions(1).Interior.Gradient.ColorStops.Add(0)
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
        End With
        With Selection.FormatConditions(1).Interior.Gradient.ColorStops.Add(1)
            .Color = 65535
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
    Next

    
End Sub

Open in new window

LVL 4
Limey1212Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

patrickabCommented:
In both instances you have only allowed for 3 variables:

    Dim RangeNum(3) As String
    Dim Formula(3) As String

when in fact you have 4 variables. Thus make them:

    Dim RangeNum(4) As String
    Dim Formula(4) As String

Patrick
0
TracyVBA DeveloperCommented:
You can also change your base array number to 1 by putting this at the top of your module:

Option Base 1

This way, you'll start your array's at 1-4, instead of 0-3, which are actually both equal to 4 options as Patrick has pointed out.  But this way, it may be a little less confusing.
0
Limey1212Author Commented:
Ok,
but the arrays are not the issue, and surely there are four in the code, (a count of objects 0-3 would be four would it not?). I've never used Option Base 1 because to me that is confusing, and not good form.

the issue I'm having is with the second line of the loop:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:=Formula(a)
this line fails obviously the way I've used the variable is incorrect, the question is how would I use it??
0
patrickabCommented:
Try using a word other than Formula...

Patrick
0
Limey1212Author Commented:
Yeah I thought of that after looking at your previous answer, but no go. Then it struck me I'm altering a protected range!!!!
Yes I AM now smacking my head on the edge of the desk as I write this. I'll be hanging my arse out for all to kick later LOL.
thanks for the second pair of eyes, I appreciate your time.
Cheers Dave
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.