Excel VBA Worksheet Calculate: Step-by-Step Guide
In this article
To run a macro automatically whenever a worksheet recalculates, open the VBA editor with Alt+F11, double-click the sheet in the Project Explorer, choose Worksheet from the left dropdown, choose Calculate from the right dropdown, and write your code inside the generated Private Sub Worksheet_Calculate() block. That event fires after Excel finishes recalculating that specific sheet, so it is the right hook for reacting to formula results rather than to typed edits. The steps below walk through enabling it safely, testing it, and fixing the problems people hit most often.
What You'll Need
- Excel on Windows or Mac with macros enabled for the workbook (the desktop app, not Excel for the web, which cannot run VBA).
- The workbook saved as .xlsm (macro-enabled) or .xlsb, because .xlsx strips VBA.
- A sheet that actually contains formulas, since Worksheet_Calculate only fires when that sheet recalculates.
- Trust settings that allow macros in your own files; if macros are blocked by policy, the event will never run.
Step-by-Step
- Save a copy first. Event code can loop or overwrite data unexpectedly. Keep a backup before you add anything.
- Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to show it; the exact wording can differ slightly by version.
- Select the correct sheet module. In the Project Explorer (View > Project Explorer if hidden), expand VBAProject, expand Microsoft Excel Objects, and double-click the sheet name, for example Sheet1. Do not use ThisWorkbook or a standard module, because Worksheet_Calculate only works in a worksheet module.
- Generate the event stub. At the top of the code window, set the left dropdown to Worksheet and the right dropdown to Calculate. Excel inserts
Private Sub Worksheet_Calculate()andEnd Sub. If you type the name manually, spelling and capitalization must match exactly or it will be treated as an ordinary macro and never fire. - Write the reaction code between the Sub and End Sub lines. A simple example that flags a value above a threshold:
If Range("B2").Value > 100 Then Range("C2").Value = "Check". Keep the code short and avoid changing cells that feed back into the same calculation. - Guard against re-entry. At the top of the procedure add
Application.EnableEvents = Falseand restore it withApplication.EnableEvents = Truebefore End Sub. This stops your own writes from triggering another calculate cycle. If an error occurs between those lines, events stay off until you reset them, so test carefully. - Close the editor and test. Return to Excel, change an input cell that a formula depends on, and press F9 to force a full recalculation. Your code should run. You can also press Ctrl+Alt+F9 for a full rebuild of dependencies if a normal recalc seems not to fire.
- Confirm it is scoped to the right sheet. Worksheet_Calculate runs only for the sheet whose module holds it. If you need the same behaviour on several sheets, copy the procedure into each sheet module, or call a shared routine in a standard module from each one.
- Save as .xlsm and reopen. Close and reopen the file to confirm the code persists and the event still fires. If Excel prompts about macros, enable them for this trusted file.
Common Mistakes
- Putting the code in a standard module or ThisWorkbook. It compiles but never runs on recalculation.
- Misspelling the procedure name, such as Worksheet_Calculte, which turns it into a dead macro.
- Forgetting to turn EnableEvents back on after an error, which makes every event in the session appear broken.
- Writing to a cell that the sheet's own formulas depend on, creating an endless recalc loop that freezes Excel.
- Using Worksheet_Calculate when you really wanted Worksheet_Change. Calculate fires on recalculation; Change fires on manual edits. They solve different problems.
- Saving as .xlsx, which silently removes the VBA project.
Tips
- Set calculation to manual while developing (Formulas > Calculation Options > Manual) so you control when the event fires.
- Use
Me.Rangeinstead ofActiveSheet.Rangeso the code always targets the sheet it lives on. - Keep the procedure fast. Heavy loops inside Calculate make every recalculation sluggish.
- Log to a cell or the Immediate window (Ctrl+G) while testing so you can see whether the event ran.
- If you only need to react to one cell, consider Worksheet_Change with an Intersect check instead; it is often cheaper than a full calculate event.
Troubleshooting
If nothing happens, first confirm macros are enabled and the file is .xlsm. Then check that the code sits in the sheet module and the name is spelled exactly. Press Ctrl+Break or use the Immediate window to run Application.EnableEvents = True in case events were left off by an earlier error. If the event fires but the wrong values appear, verify you are reading the correct cells and that calculation is not set to Manual with no recalculation happening. If Excel hangs, you likely created a feedback loop; close without saving, reopen the backup, and add the EnableEvents guard. If it works on one machine but not another, compare macro security settings and Excel versions, since labels and defaults can differ by version, device or region.
Frequently Asked Questions
Does Worksheet_Calculate fire when I type in a cell?
Only if that edit causes the sheet to recalculate. A typed value with no dependent formulas will not trigger it; use Worksheet_Change for that.
Can I use it in Excel for the web?
No. Excel for the web does not run VBA. Use the desktop app.
Why does it run many times in a row?
Your code is probably changing cells that feed the calculation. Turn off EnableEvents at the start and restore it at the end.
Does it work for other sheets?
No. Each sheet needs its own copy in its own module, or a call to a shared procedure.
Will it run when the workbook opens?
Only if opening causes that sheet to recalculate. For open-time logic, use the Workbook_Open event in ThisWorkbook.
Can I disable it temporarily?
Yes. Set Application.EnableEvents = False before your action and set it back to True afterwards.