VBA & Macro Problems

Shorten an Excel Macro for Formula-Based Conditional Formatting

Natalie TaylorNatalie Taylor Nov 22, 2026 868 views

Question details

Replace a lengthy recorded macro with concise VBA that creates three formula-based conditional formatting rules.

Shorten an Excel Macro for Formula-Based Conditional Formatting
Product
Microsoft Excel
Device & OS
not provided
Scenario
Apply green or red font and bold formatting to daily!M10:M11 based on values in the target cells and the dashb sheet.
Observed behavior
The recorded macro contains thousands of lines and produces a "Procedure too large" compile error.
Before you start

Confirm that the workbook has sheets named daily and dashb and that daily!M10:M11 is the intended range. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and enable macros if prompted.

Solution 1Recommended

Create the conditional formatting rules directly with VBA

Use FormatConditions.Add to replace the recorded actions with three concise rules.

This procedure deletes all existing conditional formatting rules on daily!M10:M11 before adding the three specified rules. It uses INDIRECT for the cross-sheet reference.

The original dashb!U13 reference is relative, so the example checks dashb!U13 for M10 and dashb!U14 for M11. If both cells should check U13, replace INDIRECT("dashb!U"&ROW()+3) with INDIRECT("dashb!U13") in both rules.

1
Open the VBA editor

In desktop Excel, open the workbook that contains the daily and dashb sheets, then select Developer > Visual Basic.

2
Insert the procedure

In the VBA editor, select the workbook project and choose Insert > Module. Paste this code into the module: Sub SetDailyFormatting() Dim target As Range Dim rule As FormatCondition Set target = ThisWorkbook.Worksheets("daily").Range("M10:M11") target.FormatConditions.Delete Set rule = target.FormatConditions.Add(Type:=xlExpression, Formula1:= _ "=AND(LEFT(INDIRECT(""dashb!U""&ROW()+3),1)<>""0"",RIGHT(M10,2)<>""--"")") rule.Font.Color = RGB(0, 128, 0) rule.StopIfTrue = False Set rule = target.FormatConditions.Add(Type:=xlExpression, Formula1:= _ "=AND(LEFT(INDIRECT(""dashb!U""&ROW()+3),1)=""0"",RIGHT(M10,2)<>""--"")") rule.Font.Color = RGB(255, 0, 0) rule.StopIfTrue = False Set rule = target.FormatConditions.Add(Type:=xlExpression, Formula1:= _ "=RIGHT(M10,2)<>""--""") rule.Font.Bold = True rule.StopIfTrue = False End Sub

3
Run and check the rules

Return to Excel and select Developer > Macros > SetDailyFormatting > Run. Select daily!M10:M11, then choose Home > Conditional Formatting > Manage Rules to inspect the formulas and formatting.

Create the conditional formatting rules directly with VBA
Existing rules are removed: FormatConditions.Delete removes every conditional formatting rule that applies to the target range. Review those rules before running the procedure.
Free Microsoft Office alternative

Manage spreadsheet formatting with WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Microsoft Excel formats, making it easier to move between office suites. It does not fix an Excel VBA compile error; check macro compatibility before migrating a macro-enabled workbook.

Use conditional formatting tools to highlight spreadsheet valuesOpen and edit Microsoft Excel XLSX and XLS filesWork in a familiar, easy-to-use spreadsheet interfaceUse a free, lightweight office suite for everyday documents
microsoft office alternative - wps office

Frequently Asked Questions

Why does Excel report "Procedure too large"?

A recorded macro can contain so many statements that the procedure exceeds Excel VBA's compilation limit. Replace repetitive recorded actions with direct calls such as FormatConditions.Add.

Will the macro keep my existing conditional formatting rules?

No. The target.FormatConditions.Delete line removes existing rules from daily!M10:M11. Review them under Home > Conditional Formatting > Manage Rules before running the macro.

Why does the example use INDIRECT for the dashb sheet?

INDIRECT lets the conditional formatting formula refer to another sheet. The expression uses ROW()+3 to preserve the relative reference: M10 checks dashb!U13, while M11 checks dashb!U14.

Can a cell be both colored and bold?

Yes. The color and bold rules set different font properties, and the macro sets StopIfTrue to False for each rule so that matching rules can all apply.