Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Worksheet Close: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To close a worksheet in Excel VBA, you don't close the sheet itself — Excel has no Worksheet.Close method. You either close the workbook that contains the sheet, or you hide the sheet so it disappears from view. The shortest working approach is ThisWorkbook.Close SaveChanges:=True to close the whole file, or Worksheets("Sheet1").Visible = xlSheetHidden to make one sheet vanish without closing the file. This guide shows both, plus how to close a sheet safely when a macro is running, how to stop the "Save changes?" prompt, and how to avoid the errors people hit most often.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm (macro-enabled) if you want your code to persist.
  • Basic familiarity with the VBA Editor: press Alt+F11 on Windows, or Option+F11 on Mac.
  • No special add-ins. Everything below uses built-in Excel objects.

Menu labels and dialog wording can differ slightly between Excel versions, platforms and regions, so if a label you see is close but not identical, you are in the right place.

Step-by-Step

  1. Open the VBA Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). The editor opens in its own window.
  2. Insert a module. In the Project Explorer, right-click your workbook name and choose Insert > Module. Code in a standard module is easiest to test.
  3. Decide what "close" means for your task. If you want the whole file gone, close the workbook. If you want just one tab out of sight while the file stays open, hide the sheet. VBA cannot close a single worksheet on its own.
  4. Close the workbook that holds the sheet. Use:
    ThisWorkbook.Close SaveChanges:=True
    Replace ThisWorkbook with Workbooks("Book1.xlsx") if you are closing a different file. SaveChanges:=True saves first; use False to discard edits, or omit the argument to let Excel prompt the user.
  5. Hide a single sheet instead. Use:
    Worksheets("Sheet1").Visible = xlSheetHidden
    This removes the tab from view but keeps the data. To bring it back, set the property to xlSheetVisible.
  6. Close every workbook except the one running the macro. Loop through the collection:
    Dim wb As Workbook
    For Each wb In Workbooks
      If Not wb Is ThisWorkbook Then wb.Close SaveChanges:=False
    Next wb
  7. Suppress the save prompt. Set Application.DisplayAlerts = False before closing and back to True immediately after. This stops the "Do you want to save?" dialog from blocking an unattended macro.
  8. Run the macro. Place the cursor inside the procedure and press F5, or assign it to a button via Developer > Insert > Button (Form Control).
  9. Save your work. Press Ctrl+S in the editor, then close the editor and save the workbook as .xlsm.

Common Mistakes

  • Trying Worksheets("Sheet1").Close. This method does not exist and throws a compile or runtime error. Use workbook close or sheet hide instead.
  • Forgetting to reset DisplayAlerts. If your macro errors out before the reset line, Excel stays silent about alerts for the rest of the session. Put the reset in an error handler or use On Error Resume Next carefully around just the close call.
  • Closing ThisWorkbook from code stored inside it. The macro stops the moment the workbook closes, so any lines after the close never run. Do cleanup first, then close last.
  • Hiding the last visible sheet. Excel refuses to hide every sheet — at least one must stay visible. Unhide another sheet first.
  • Using ActiveWorkbook when several files are open. The active workbook may not be the one you expect. Name the workbook explicitly.
  • Closing a workbook with unsaved changes and SaveChanges:=False by accident. That discards edits with no prompt. Double-check the argument before running.

Tips

  • Use ThisWorkbook for code that should always act on the file it lives in, and Workbooks("name.xlsx") when acting on another file.
  • If you only want to hide a sheet temporarily, store its previous visibility in a variable so you can restore it exactly (it might have been xlSheetVeryHidden).
  • xlSheetVeryHidden hides a sheet so thoroughly that the right-click Unhide menu cannot reveal it — only VBA can. Useful for config sheets, but easy to forget.
  • Wrap close routines in On Error GoTo handlers so a missing workbook name doesn't leave alerts switched off.
  • Test on a copy of the workbook the first time. Closing with SaveChanges:=True overwrites the file immediately.

Troubleshooting

"Subscript out of range" when naming a sheet. The sheet name has a typo or the sheet lives in a different workbook. Check the exact tab name, including spaces, and qualify it with the workbook.

"Method 'Close' of object 'Workbook' failed." Usually another macro or an open dialog is holding the file. Close dialogs, stop any running code with the Reset button in the VBA Editor, then retry.

The macro seems to do nothing. Check that events and screen updating aren't masking the result, and confirm the procedure actually ran — add a MsgBox "done" at the end while testing.

The save prompt still appears. DisplayAlerts only suppresses the prompt if it is set before the close call and the workbook isn't in a state Excel considers protected (for example, a protected structure). Unprotect the workbook structure first if needed.

Excel hangs after closing. A leftover reference to a closed workbook can cause this. Set object variables to Nothing after use.

If that did not work, save your code, close Excel completely, reopen the file, and run the macro again. If it still fails, step through the code line by line with F8 to see exactly which statement errors.

Frequently Asked Questions

Can VBA close a single worksheet without closing the workbook?

No. Excel has no method to close one sheet. You can hide it with Visible = xlSheetHidden, or delete it with Worksheets("Sheet1").Delete, which removes the data permanently.

How do I close a workbook without the save prompt?

Use Workbooks("file.xlsx").Close SaveChanges:=False to discard changes, or SaveChanges:=True to save silently. Setting Application.DisplayAlerts = False around the call also suppresses the dialog.

Why does my macro stop after closing the workbook?

If the code lives in the workbook being closed, the macro ends with it. Move the close call to the last line, or run the macro from a separate add-in or personal workbook.

Can I close a sheet when the workbook is protected?

Hiding or deleting sheets is blocked while the workbook structure is protected. Unprotect it first with the correct password, or the operation fails.

What is the difference between xlSheetHidden and xlSheetVeryHidden?

xlSheetHidden hides the tab but users can unhide it from the right-click menu. xlSheetVeryHidden removes it from that menu entirely, so only VBA can make it visible again.

Does closing a workbook in VBA lose unsaved changes?

Only if you pass SaveChanges:=False. Omitting the argument lets Excel ask the user, and True saves first. Always choose deliberately in unattended macros.