Create conditional formatting to indent data by VBA gives runtime error on ExecuteExcel4Macro

Edgar62

New Member
Joined
Nov 30, 2015
Messages
2
Hi!

I am trying to create conditional formatting rules via VBA. I often use a custom number format to indent lines of text based on some level indicator. The custom number format is then something like '" "@' to indent by 3 blanks. Then I can embed this within a conditional format with conditions like '=($A1=3)' which is then applied to format text columns according to the level present in column A.

If I am recording this as a macro, the following code is generated:

Code:
Sub IndentText()
'
' IndentText Macro
'


'
    Range("B1:B7").Select
    Selection.NumberFormat = """   ""@"
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=($A1=3)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ExecuteExcel4Macro "(2,1,""""   ""@"")"
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

When I am trying to execute the macro there is a runtime error on the line with the ExecuteExcel4Macro. (BTW: I am trying this on Excel 2016)

How can I solve this?

Edgar
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi!

I am trying to create conditional formatting rules via VBA. I often use a custom number format to indent lines of text based on some level indicator. The custom number format is then something like '" "@' to indent by 3 blanks. Then I can embed this within a conditional format with conditions like '=($A1=3)' which is then applied to format text columns according to the level present in column A.

If I am recording this as a macro, the following code is generated:

Code:
Sub IndentText()
'
' IndentText Macro
'


'
    Range("B1:B7").Select
    Selection.NumberFormat = """   ""@"
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=($A1=3)"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    ExecuteExcel4Macro "(2,1,""""   ""@"")"
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

When I am trying to execute the macro there is a runtime error on the line with the ExecuteExcel4Macro. (BTW: I am trying this on Excel 2016)

How can I solve this?

Edgar

Hi!

Actually it was easy to fix by not using the macro recorder:

Instead of the ExecuteExcel4Macro call, I use now
Code:
   Range("B1:B7").Select
    For Level = 1 To 3
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=($A1=" & Level & ")"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        ' Indentation by Level
        Selection.FormatConditions(1).NumberFormat = """" & String(Level, " ") & """@"
        Selection.FormatConditions(1).StopIfTrue = False
    Next Level

So this thread can be closed.

Edgar
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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