Excel VBA: Write Subtraction Formulas and Highlight Nonzero Results
Question details
Update a VBA procedure to write receipt and payment differences as worksheet formulas and highlight results that are not zero.

- 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.
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.
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.
In Excel, open the VBA editor from Developer > Visual Basic, then open the module containing the existing procedure.
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)
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.

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.

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.




