VBA & Macro Problems

Excel VBA: Write Subtraction Formulas and Highlight Nonzero Results

Rana GarciaRana Garcia Dec 2, 2026 868 views

Question details

Update a VBA procedure to write receipt and payment differences as worksheet formulas and highlight results that are not zero.

Write Subtraction Formulas and Highlight Nonzero Results in Excel VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro finds receipt and payment totals dynamically and writes difference checks to a worksheet.
Observed behavior
The desired result is for both difference cells to contain formulas and receive conditional formatting when their values are nonzero.
Before you start

Open the workbook and locate the existing procedure in the VBA editor. Confirm that ws, check, receiptsLastrow, paymentsLastrow, contractbid, and vendortotal are set before the difference checks run, and that the referenced totals are on ws.

Solution 1Recommended

Write formulas and apply nonzero rules to the target cells

Use worksheet formulas for both differences and add cell-value rules directly to their destination cells.

The formulas refer to totals on the same worksheet as the destination cells. Applying rules to the target ranges avoids relying on the current selection.

1
Open the procedure

In Excel, open the VBA editor from Developer > Visual Basic, then open the module containing the existing procedure.

2
Add the formulas and rules

In the procedure, after the variables and ranges have been assigned, add the following code. Place the declarations with the procedure's other declarations: Dim receiptCell As Range Dim paymentCell As Range Dim fc As FormatCondition Set receiptCell = ws.Range("G" & check + 3).Offset(0, 1) Set paymentCell = ws.Range("G" & check + 4).Offset(0, 1) receiptCell.Formula = "=B" & receiptsLastrow & "-" & contractbid.Offset(0, 1).Address(False, False) paymentCell.Formula = "=C" & paymentsLastrow & "-" & vendortotal.Offset(0, 3).Address(False, False) Set fc = receiptCell.FormatConditions.Add(Type:=xlCellValue, Operator:=xlNotEqual, Formula1:="=0") fc.Interior.Color = RGB(255, 255, 0) Set fc = paymentCell.FormatConditions.Add(Type:=xlCellValue, Operator:=xlNotEqual, Formula1:="=0") fc.Interior.Color = RGB(255, 255, 0)

3
Run and verify the checks

Run the procedure from the VBA editor, then select each destination cell in the worksheet and inspect its formula in the formula bar. Check that a nonzero result is highlighted and a zero result is not.

Write formulas and apply nonzero rules to the target cells
Existing formatting rules: Each run adds new rules. Review existing rules under Home > Conditional Formatting > Manage Rules; remove duplicates if needed. Deleting all rules from a target cell would also remove any unrelated conditional formatting.
Free Microsoft Office alternative

Work with spreadsheet formulas in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for working with formulas and conditional formatting. Its Excel-format compatibility can make migration easier, although Excel VBA macros may require separate compatibility checks.

Create subtraction formulas and apply conditional formatting in WPS SpreadsheetsOpen and edit common Microsoft Excel formats, including XLSXUse a familiar interface for everyday spreadsheet workMove existing spreadsheets to a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why use .Formula instead of assigning a calculated value?

Assigning a string beginning with = to the target cell's .Formula property stores a worksheet formula, so Excel can recalculate the difference when its referenced totals change.

What does xlNotEqual with Formula1 set to =0 do?

With Type:=xlCellValue, the rule compares the target cell's value with zero and applies its formatting when the value is not equal to zero.

Why should the macro format the target range instead of Selection?

Selection depends on what is currently selected in Excel. Using receiptCell.FormatConditions or paymentCell.FormatConditions applies each rule to the intended cell regardless of the selection.

Will running the macro again create duplicate rules?

Yes. FormatConditions.Add creates a new rule each time it runs. Inspect or remove duplicates through Home > Conditional Formatting > Manage Rules.

Will these formulas work if the referenced totals are on another sheet?

The example assumes the totals are on ws. If a referenced total is on another sheet, the formula must include a properly formatted sheet reference.