To work on every sheet in an Excel workbook with VBA, loop through the Worksheets collection with a For Each ws In ThisWorkbook.Worksheets block and put your action inside the loop. This one pattern handles formatting, printing, saving, exporting and bulk edits for every tab at once, including tabs you add later, as long as the code runs after they exist. Nothing extra is needed beyond Excel itself, but you do need to know how to open the VBA editor and save a workbook in a macro-enabled format.
What You'll Need
- Excel on Windows or Mac with macros enabled (the Developer tab is hidden by default and has to be turned on first).
- The workbook saved as
.xlsmor.xlsb, because standard.xlsxfiles cannot store macros. - A little care about which sheets are protected, hidden, or linked to other data before you start overwriting cells.
- A backup copy of the file. Bulk edits across all sheets are hard to undo with Ctrl+Z.
Step-by-Step
- Enable the Developer tab. Right-click the ribbon area and choose Customize the Ribbon, then tick Developer in the list on the right. On Mac, open Excel > Preferences > Ribbon & Toolbar and tick Developer. Labels vary slightly by version, so look for the Developer entry in the ribbon customisation list.
- Open the editor and add a module. Click Developer > Visual Basic, then in the editor choose Insert > Module. Code in a standard module is easier to reach from the Macro dialog than code in a sheet's own module.
- Write the basic loop. Type a short procedure such as:
Sub FormatAllSheets()Dim ws As WorksheetFor Each ws In ThisWorkbook.Worksheetsws.Cells.Font.Name = "Calibri"ws.Cells.Font.Size = 11Next wsEnd Sub
Everything between theFor EachandNextlines runs once per sheet, withwspointing at the current one. - Choose
ThisWorkbookoverActiveWorkbook.ThisWorkbookalways refers to the file containing the code, so the macro keeps working if another workbook happens to be in front. UseActiveWorkbookonly when you genuinely mean whatever file is active. - Skip sheets you do not want touched. Add a guard inside the loop, for example
If ws.Name = "Summary" Then GoTo SkipSheetfollowed later bySkipSheet:at the end of the loop body, or simply checkws.Visibleto leave hidden sheets alone. This matters when a cover page or instruction tab must stay untouched. - Run it. Press F5 inside the procedure, or return to Excel and use Developer > Macros, select the procedure name and click Run. The macro finishes almost instantly on normal workbooks; large ones with heavy formulas may take a moment.
- Decide what happens when nothing is selected. If your action works on the selection instead of the whole sheet, remember that only one sheet has a selection at a time. Refer to
ws.Range("A1:Z100")orws.UsedRangeexplicitly rather thanSelection. - Extend the pattern to common jobs. To set print settings:
ws.PageSetup.Orientation = xlLandscape. To autofit columns:ws.Columns.AutoFit. To export each sheet as a PDF, callws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=...inside the loop, building a unique filename fromws.Nameso files do not overwrite each other. - Test on a copy first. Run the macro on a duplicate workbook, check a few sheets, then run it on the real file. If the code deletes rows, clears contents or writes values, the original data is not recoverable from the saved file once you save over it.
Common Mistakes
Looping over Sheets and calling worksheet-only properties. Sheets includes chart sheets, which have no cell range. Use Worksheets when your code touches cells.
Using Select and Activate inside the loop. Switching the visible sheet for every tab slows the macro badly and can fail if the workbook window is not active. Work with the object reference instead: ws.Range("A1").Value = ....
Forgetting protected sheets. Writing to a protected sheet raises an error unless protection is temporarily removed. If you own the password, unprotect and re-protect inside the loop; never attempt to break protection on a file you do not control.
Overwriting export files. If every iteration writes to the same filename, only the last sheet survives. Include ws.Name in the path.
Sheet names with characters illegal in filenames. Names cannot normally contain slashes or colons, but build in a quick replace if you rename sheets programmatically.
Saving as .xlsx. The macro is discarded silently on save. Keep the file as .xlsm.
Tips
- Wrap the loop in
Application.ScreenUpdating = Falseand set it back toTrueat the end for a noticeable speed gain on big workbooks. - Add
On Error GoTohandling that reports the sheet name, so a failure tells you which tab caused it. - Work backwards with a numeric loop,
For i = Worksheets.Count To 1 Step -1, when the code deletes sheets; deleting forward skips items. - Put the action in a separate procedure and call it from the loop, so you can test one sheet at a time by pointing at
Worksheets("Sheet1"). - Comment the top of the macro with what it changes, so anyone opening the file later knows.
Troubleshooting
"Subscript out of range" or "Object doesn't support this property". Usually a chart sheet or a misspelled sheet name. Check the loop variable type is Worksheet and that you looped Worksheets.
"Method 'Range' of object failed". Often a protected sheet or a hidden one your code tries to activate. Remove the activation, or handle protection first.
The macro does not appear in the Macro list. It may be in a sheet module, marked Private, or take arguments. Move it to a standard module and declare it as Public Sub with no parameters.
Nothing happens when you run it. Check the workbook actually contains more than one visible sheet and that your guard condition is not skipping everything.
Excel warns about macros being disabled. Enable macros for that file through the security bar or Trust Center settings, having confirmed the file is yours or from a trusted source. Do not lower security settings globally to run one file.
If that did not work, save and reopen the workbook, then run the macro once on a fresh copy. Corrupted state and leftover selections are common causes of odd errors.
Frequently Asked Questions
How do I loop through all sheets in an open workbook other than the one with the code?
Set a workbook variable: Dim wb As Workbook, Set wb = Workbooks("Book2.xlsx"), then loop For Each ws In wb.Worksheets. The file must be open.
How do I apply the same value to a cell on every sheet?
Inside the loop use ws.Range("B2").Value = "Draft". Each sheet gets its own B2; there is no cross-sheet range in Excel.
Can I include hidden sheets?
Yes. Worksheets includes hidden sheets, so filter with If ws.Visible = xlSheetVisible Then if you want visible ones only.
How do I combine data from every sheet into one?
Loop each sheet and copy its used range into the next empty row of a summary sheet, incrementing a row counter. Use ws.UsedRange and copy values only to avoid dragging formatting.
Does this work in Excel for Mac?
Yes, the object model and loop syntax are the same. Ribbon and menu locations differ slightly, and some file-format export options behave differently between platforms.
Why does my macro stop on the first sheet?
An unhandled error ends the procedure immediately. Add error handling that logs ws.Name before the failing line so you can see which sheet triggered it.