VBA macros · Utilities

VBA macro to set all sheets to 100% zoom and select A1

Tidy a workbook before sending it: every sheet at 100% zoom, scrolled to the top with A1 selected, and the first sheet active. One macro, any workbook.

Windows + Mac

A workbook that opens on sheet 7, scrolled to row 4,000 at 55% zoom with a random block selected looks careless. Many people tidy every sheet by hand before sending a report. This macro does it in a second: each visible sheet is set to 100% zoom, scrolled to the top-left corner with A1 selected, and the workbook ends on its first visible sheet.

VBA
Option Explicit

Sub ZoomAllSheetsSelectA1()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim first As Worksheet

    Set wb = ActiveWorkbook
    If wb Is Nothing Then Exit Sub

    For Each ws In wb.Worksheets
        If ws.Visible = xlSheetVisible Then
            ' Goto activates the sheet and scrolls A1 to the top-left corner
            Application.Goto ws.Range("A1"), Scroll:=True
            ActiveWindow.Zoom = 100
            If first Is Nothing Then Set first = ws
        End If
    Next ws

    If Not first Is Nothing Then first.Activate
End Sub
Reset zoom, scroll position and selection on every sheet

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick ZoomAllSheetsSelectA1 and click Run.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

Zoom, scroll position and selection are not properties of a worksheet; they belong to the window that shows it. That is why this is one of the rare macros where activating each sheet is genuinely necessary: the window settings can only be changed for the sheet that is on screen.

Application.Goto ws.Range("A1"), Scroll:=True does three things at once: it activates the sheet, selects A1 and scrolls the window so A1 is in the top-left corner. Plain Range("A1").Select would select the cell without scrolling, leaving the view somewhere in the middle of the sheet. After that, ActiveWindow.Zoom = 100 sets the zoom for the sheet that is now active.

Hidden sheets are skipped because they cannot be activated. The first visible sheet is remembered in first and activated at the end, so the workbook opens on it next time once you save. Screen updating is deliberately left on: window settings are visual, and with updating off some versions do not store the new scroll position.

Change it

Change 100 to any zoom level between 10 and 400, or use ActiveWindow.Zoom = 85 for a consistent smaller view. To return each sheet to a fixed cell other than A1, for example the first data cell, use ws.Range("B3") in the Goto line.

To save the workbook straight away so the clean view is what the recipient sees, add wb.Save as the last line. If you want the same view for grid lines and headings too, add ActiveWindow.DisplayGridlines = True (or False) and ActiveWindow.DisplayHeadings = True inside the loop; both are window settings just like zoom.

Frozen panes are respected: Goto scrolls the unfrozen part to the top, and the frozen rows stay where they are. To remove freezing everywhere, add ActiveWindow.FreezePanes = False inside the loop.

What can go wrong

If the workbook has several windows open (View › New Window), the macro changes only the active window. Close the extra windows first. Chart sheets are not touched because the loop runs over Worksheets.

Watch out. If any sheet is protected with Select locked cells switched off, selecting A1 fails with error 1004. Unprotect it first with VBA macro to protect or unprotect all sheets at once in Excel or unlock A1 on that sheet.
Tip. The macro works on Windows and Mac and is a good candidate for the Personal Macro Workbook plus a button on the Quick Access Toolbar (File › Options › Quick Access Toolbar, choose Macros), because you will use it on many files.

Related: VBA macro to auto-fit all columns on all sheets in Excel, VBA macro to reset the used range and clear excess formatting.

More vba macros