Help with VBA creating new conditional formatting rules

braindiesel

Well-known Member
Joined
Mar 16, 2009
Messages
571
Office Version
  1. 365
  2. 2019
  3. 2010
Platform
  1. Windows
I am running ito an error re ExcecuteExcel4MAcro line
This code was produced by the recorder.

Any ideas?
VBA Code:
Range("L5").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=MATCH(F5,myFactors,0)>=6"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ExecuteExcel4Macro "(2,1,""0 x"")"
    Selection.FormatConditions(1).StopIfTrue = False
    Range("L5").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=MATCH(F5,myFactors,0)=3"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ExecuteExcel4Macro "(2,1,""0""'' Centers"""")"
    Selection.FormatConditions(1).StopIfTrue = False
    Range("L5").Select
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Assuming that your Excel 4 macros work when you run them manually ,then you are probably running into syntax issues with the ExecuteExcel4Macro method.


Excel 4 macros are a pain. Do yourself a favor and convert them to VBA macros.
 
Upvote 0
What are those xl4 macros doing?
 
Upvote 0
Copying and pasting wasn't pasting the conditional formatting so I decided to use the Macro recorder to rewrite the conditional formatting from scratch and it wrote the code this way... and the code unedited does not work. Thus, I am here.

The intent is that based on an adjacent cell, I want the targeted cell formatted.
If the adjacent cell says "Centers"and they enter a 14, I want the cell to say 14'' Centers but the ability is retained to use the 14 for mathematical calculations.
I have 4 rules in total for the cell...... I only copied the code for two as the other two are similar
It has worked without issue when written from scratch... but when a user deletes it I want code to rewrite it. Thus I am here.
 
Upvote 0
Are you saying that the macro recorder recorded the use of the XL4 macro?
If so how did you change the cell format?
 
Upvote 0
I have been using Excel and VBA for years. I did not intentionally do anything to change this... I had an update in the past few days... not sure if something internal triggered this.

But yes... the macro recorder recorded the use of the XL4 Macro... and I do not know why.
 
Upvote 0
Ok, delete the xl4macro lines & replace them with something like
VBA Code:
    Selection.FormatConditions(1).NumberFormat = "0""'' Center"""
 
Upvote 0
VBA Code:
    With Range("L5")
        .FormatConditions.Delete
        With Range("L5").FormatConditions.Add(Type:=xlExpression, Formula1:="=MATCH(F5,myFactors,0)>=6")
            .NumberFormat = "# ""x"""
            .StopIfTrue = False
        End With

        With Range("L5").FormatConditions.Add(Type:=xlExpression, Formula1:="=MATCH(F5,myFactors,0)=3")
            .SetFirstPriority
            .NumberFormat = "# ""'' centers"""
            .StopIfTrue = False
        End With
        .Select
    End With
 
Upvote 0
I never replied back... apologies... This worked. Thank you
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top