Excel VBA Refresh Worksheet: Step-by-Step Guide
In this article
To refresh a worksheet in Excel VBA, you force Excel to recalculate formulas and, if the sheet contains external data connections, refresh those connections. The shortest working method is: open the VBA editor with Alt+F11, insert a module, and write a macro that calls ActiveSheet.Calculate for formulas or ActiveWorkbook.RefreshAll for data connections. Run it with F5. This guide walks through both approaches, explains when each is appropriate, and covers the mistakes that stop refreshes from working.
What You'll Need
- Excel for Windows or Mac with macros enabled (the Developer tab visible).
- A workbook saved as .xlsm (macro-enabled) or .xlsb, because .xlsx cannot store VBA code.
- Basic familiarity with the VBA editor: Alt+F11 opens it, Ctrl+G opens the Immediate window.
- If you are refreshing external data, the connection must already be set up and working manually (Data > Refresh All).
Menu labels in Excel can differ slightly by version, platform and region. The steps below assume a desktop version of Excel; Excel for the web does not run VBA macros at all.
Step-by-Step
- Save the workbook as macro-enabled. Go to File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). If you skip this, Excel will warn you on save and your macro will be lost.
- Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). The editor opens in a separate window.
- Insert a standard module. In the editor, choose Insert > Module. A blank code pane appears. Do not paste code into the ThisWorkbook or a sheet module unless you specifically want event-driven code.
- Write the refresh macro for formulas. Type:
Sub RefreshSheet()
ActiveSheet.Calculate
End Sub
This recalculates every formula on the active sheet. UseApplication.Calculateinstead if you want to recalculate all open workbooks. - Write the refresh macro for data connections. If your sheet pulls from an external source (a database, a web query, another workbook), use:
Sub RefreshConnections()
ActiveWorkbook.RefreshAll
End Sub
RefreshAll refreshes every connection in the workbook. To refresh only one, name the connection:ActiveWorkbook.Connections("YourConnectionName").Refresh. - Combine both if needed. A sheet that has formulas depending on refreshed data needs the refresh first, then the calculation:
Sub FullRefresh()
ActiveWorkbook.RefreshAll
Application.CalculateFull
End SubCalculateFullforces a complete recalculation, which is slower but guarantees every dependent formula updates. - Run the macro. Press F5 while the cursor is inside the subroutine, or go back to Excel and use Developer > Macros, select the macro name and click Run. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to enable it.
- Test on a copy. Before running against live data, save a duplicate of the workbook and run the macro there. Confirm the values change as expected and no error dialog appears.
- Assign the macro to a button (optional). On the sheet, go to Developer > Insert > Button (Form Control), draw the button, and assign your macro in the dialog. This lets anyone refresh without opening the VBA editor.
Common Mistakes
- Saving as .xlsx. The macro runs once but disappears when the file is closed. Always use .xlsm.
- Putting the code in the wrong module. Code in a sheet module only runs on that sheet's events. Standard modules are for general macros.
- Using Calculate when the problem is stale external data. Calculate only recomputes formulas; it does not re-query a database or web source. Use RefreshAll for that.
- Assuming RefreshAll is synchronous. Some connections refresh in the background, so the next line of code may run before data arrives. If order matters, set the connection to refresh synchronously in its properties, or add a wait loop that checks a cell value.
- Forgetting that macros must be enabled. If the ribbon shows a security warning, click Enable Content. Macros in files from the internet are blocked by default; use the Trust Center to allow a trusted location rather than lowering security globally.
- Refreshing a protected sheet. If the worksheet is protected, Calculate may fail. Unprotect it in code with the correct password before refreshing, then reprotect it.
Tips
- Turn off screen updating while the macro runs to speed it up:
Application.ScreenUpdating = Falseat the start andTrueat the end. Always restore it, even if an error occurs. - Use
ThisWorkbookinstead ofActiveWorkbookwhen the macro should always act on the file it lives in, regardless of which workbook is in front. - Name your connections clearly in Data > Queries & Connections so you can refresh one by name instead of all.
- Add a timestamp cell in the macro (for example, write
Nowto a cell) so users can see when the data was last refreshed. - Keep refresh macros short and single-purpose. A macro that refreshes, calculates, formats and emails is hard to debug.
Troubleshooting
The macro does nothing. Check that macros are enabled and that you are running the correct subroutine. Press Ctrl+G in the VBA editor and type the macro name followed by Enter to run it directly and see any error.
Compile error or "Expected: end of statement". This usually means a smart quote or a missing line break was pasted in. Retype the code rather than copying from a web page, or replace curly quotes with straight ones.
RefreshAll gives a "connection not found" error. The connection name in your code does not match the workbook. Open Data > Queries & Connections and copy the exact name.
Data refreshes but formulas show old values. Add Application.CalculateFull after the refresh, or check that calculation is set to Automatic in Formulas > Calculation Options.
The macro is slow or Excel freezes. Large external queries can take time. Disable screen updating, avoid refreshing all connections when one will do, and test with a smaller data set first.
"Macros have been disabled" bar appears every time. Move the workbook to a folder marked as a Trusted Location in File > Options > Trust Center > Trust Center Settings > Trusted Locations. Do not disable macro security entirely.
If none of that works, close Excel completely, reopen the workbook, and try the macro on a fresh copy. If the file itself is damaged, open it with Excel's Open and Repair option before adding code back.
Frequently Asked Questions
What is the difference between Calculate and RefreshAll in VBA?
Calculate recomputes formulas already in the workbook. RefreshAll re-queries external data sources such as databases or web queries. If your numbers come from outside Excel, you need RefreshAll; if they come from formulas, Calculate is enough.
Can I refresh just one worksheet instead of the whole workbook?
Yes for formulas: Worksheets("Sheet1").Calculate recalculates only that sheet. For external connections, refresh the specific connection by name rather than calling RefreshAll.
Why does my macro disappear after I save?
The file was saved as .xlsx, which cannot store VBA. Save as .xlsm and the code will persist.
Does VBA work in Excel for the web or on mobile?
No. VBA macros run only in the desktop versions of Excel for Windows and Mac. The web and mobile apps can open macro-enabled files but cannot run the code.
How do I refresh data automatically when the workbook opens?
Put ThisWorkbook.RefreshAll in the Workbook_Open event in the ThisWorkbook module. Note that this runs only if macros are enabled, and users will see a security prompt.
Can I refresh a worksheet on a timer?
Yes, using Application.OnTime to schedule a macro at a set interval. Keep intervals reasonable, because each refresh consumes resources and frequent external queries may hit source limits.