VBA & Macro Problems

Automate Conditional Formatting Results with an Excel Macro

Natalie TaylorNatalie Taylor Nov 27, 2026 869 views

Question details

Automatically identify numbers that meet conditional formatting logic and place them beside their corresponding combinations.

Automate Conditional Formatting Results with an Excel Macro
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet contains combinations and numbers evaluated by conditional formatting rules.
Observed behavior
The user currently copies the selected numbers manually and wants to automate the process.
Before you start

In Excel, open Home > Conditional Formatting > Manage Rules and note the rule formulas and ranges. Identify the columns containing each combination and candidate number, plus an empty output column.

Solution 1Recommended

Use helper formulas and a VBA macro to copy matching numbers

Reproduce the conditional formatting tests in helper columns, then use a macro to write matching values to an output column.

The example assumes each row contains a combination in column A and one candidate number in column B. Helper columns BA through BC test that row, BD combines the results, and BE receives the matching number. Change these columns and the starting row to match your workbook.

The formulas capture the rule logic rather than the displayed cell color. If rule priority, Stop If True, or non-formula rules affect the formatting, account for those conditions when building the helper formulas.

1
Inspect the formatting rules

Select the formatted cells and open Home > Conditional Formatting > Manage Rules. Note each rule's formula and Applies to range, including any relative references.

2
Create helper formulas

Enter a formula in BA3 that reproduces the first rule for row 3. Put other rule tests in BB3 and BC3, adjusting references so each formula evaluates the intended cells in its row. Fill the formulas down through the last data row.

3
Combine the tests

Enter =OR(BA3,BB3,BC3) in BD3 and fill it down. This returns TRUE when at least one helper test is TRUE; adjust the formula if your selection requires different logic.

4
Add the macro

Open Developer > Visual Basic, select Insert > Module, and paste this example. It uses the active worksheet and overwrites values in BE3 through the last used row of column A: Sub CopyMatchingNumbers() Dim ws As Worksheet Dim lastRow As Long Dim r As Long Set ws = ActiveSheet lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row For r = 3 To lastRow If ws.Cells(r, "BD").Value = True Then ws.Cells(r, "BE").Value = ws.Cells(r, "B").Value Else ws.Cells(r, "BE").ClearContents End If Next r End Sub

5
Save and run the macro

Use File > Save As to save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Select the intended worksheet, then open Developer > Macros, select CopyMatchingNumbers, and click Run. Enable macros only if you trust the workbook.

6
Check the results

Compare the values in column BE with the highlighted candidates and the TRUE/FALSE results in BD. If they differ, revisit the rules and helper formula references in Home > Conditional Formatting > Manage Rules.

Use helper formulas and a VBA macro to copy matching numbers
Adapt the example to your workbook: The macro handles one candidate number per row. Multiple candidates per combination or a different output layout require changes to the formulas and VBA code.
Free Microsoft Office alternative

Work with spreadsheet formulas in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and use formulas and conditional formatting while moving your everyday spreadsheet work to WPS.

Use helper formulas to evaluate spreadsheet conditionsOpen and edit Microsoft Excel XLSX and XLS filesNavigate a familiar, easy-to-use spreadsheet interfaceMove everyday spreadsheet work to a lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why use helper columns instead of reading the cell color?

Helper formulas expose the conditions as TRUE or FALSE values that VBA can read. They also avoid relying on the displayed color, though the formulas must accurately reproduce the applicable rules.

How do I find the formula behind a conditional formatting rule?

Select the formatted range and open Home > Conditional Formatting > Manage Rules. Select a rule and choose Edit Rule to inspect its settings and formula, if it uses one.

What does =OR(BA3,BB3,BC3) do?

It returns TRUE if at least one of the three helper cells is TRUE. Enter it in BD3 and fill it down so each row combines its own test results.

Will the example macro work if a combination has several candidate numbers?

Not without changes. The example copies one candidate from column B per row; the source ranges and output layout must be specified before adapting it for multiple numbers.

Why do I need to save the file as XLSM?

The Excel Macro-Enabled Workbook format preserves VBA code. Choose File > Save As and select Excel Macro-Enabled Workbook before closing the file.