VBA & Macro Problems

Fix an Undefined Variable in an Excel VBA Worksheet Event

John WilsonJohn Wilson Nov 14, 2026 868 views

Question details

The user needs an Excel VBA event macro to detect Yes formula results in column Z and display a reminder without an undefined-variable error.

Fix an Undefined Variable in an Excel VBA Worksheet Event
Product
Microsoft Excel
Device & OS
not provided
Scenario
Columns W through Z contain formulas, and the user wants a reminder when a cell in column Z returns Yes.
Observed behavior
The macro stops at ReferralsCalled because the variable has not been declared. Worksheet_Change also does not run when only a formula result changes.
Before you start

Identify the worksheet and the exact range in column Z that should trigger the reminder. Decide whether the reminder should appear once while any cell contains Yes or every time the sheet recalculates.

Solution 1Recommended

Declare the flag and check formula results with Worksheet_Calculate

Use this approach when column Z contains formulas and you want one reminder while at least one cell in the selected range displays Yes.

Worksheet_Change responds to cell edits, not to changes in formula results alone. Worksheet_Calculate runs after the worksheet recalculates and does not provide a Target argument for Intersect.

The example resets the flag when no cells in the range contain Yes, allowing a new reminder if Yes appears again. The public flag is shared across the VBA project and resets when the VBA project resets.

1
Declare the public flag

Open the VBA editor with Alt+F11. Choose Insert > Module, then add this line outside any procedure: Public ReferralsCalled As Boolean

2
Add the worksheet event

In the Project pane, double-click the worksheet containing the formulas. Add the following code to its worksheet module, adjusting Z2:Z1000 to match your range: Private Sub Worksheet_Calculate() Dim hasYes As Boolean hasYes = Application.CountIf(Me.Range("Z2:Z1000"), "Yes") > 0 If hasYes And Not ReferralsCalled Then ReferralsCalled = True MsgBox "A referral needs a call." ElseIf Not hasYes Then ReferralsCalled = False End If End Sub

3
Save and test the macro

Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Enable macros if prompted and you trust the file, then change an input that causes column Z to recalculate to Yes. Confirm that the message appears and that it can appear again after all Yes results disappear and a new one appears.

Declare the flag and check formula results with Worksheet_Calculate
Existing event procedures: A worksheet can have only one Worksheet_Calculate procedure. If one already exists in that worksheet module, integrate the check into it rather than adding a second procedure.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Excel files. It supports common Excel formats for easier migration, though VBA event compatibility depends on the WPS edition and configuration and should be tested separately.

Review formulas and results in a familiar spreadsheet interfaceOpen and edit common Microsoft Excel formats, including XLSXMove existing spreadsheets into a lightweight office suiteUse WPS Office for free
microsoft office alternative - wps office

Frequently Asked Questions

Why is ReferralsCalled undefined in my macro?

The variable has not been declared in a scope the event can access. In the VBA editor, choose Insert > Module and add Public ReferralsCalled As Boolean outside any procedure.

Why doesn't Worksheet_Change detect a new formula result?

Worksheet_Change runs when cells are changed, not when a formula's displayed result changes by recalculation alone. Put a Worksheet_Calculate procedure in the relevant worksheet module to check formula results.

Can I use Intersect with Worksheet_Calculate?

Worksheet_Calculate has no Target argument, so Intersect(Target, ...) cannot identify a changed cell in that event. Instead, check a defined range such as Me.Range("Z2:Z1000") after recalculation.

Why does the reminder appear again after I reopen the workbook?

A public VBA variable is not a permanent stored value. ReferralsCalled resets when the VBA project resets, so a later recalculation can display the reminder again if the range still contains Yes.

Where should I save the event code?

In the VBA editor's Project pane, double-click the worksheet containing the formulas and place Worksheet_Calculate in that worksheet's module. Keep the Public variable declaration in a standard module.