Edit an Excel VBA Macro to Check and Delete the Home Worksheet
Question details
The user needs to edit a recorded VBA macro so it checks whether the Home worksheet exists before deleting it and then continues with the remaining macro instructions.
- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro may be run more than once, so the Home worksheet might already have been deleted when the code reaches the deletion step.
- Observed behavior
- The macro attempts to delete or reference the Home worksheet without first confirming that the worksheet exists, which can interrupt later macro execution.
Open the workbook in the VBA editor and confirm that the worksheet name is exactly Home. Make sure the workbook contains at least one other visible worksheet, because Excel cannot delete its only visible sheet.
Add a worksheet existence check before deleting Home
Use this approach when the macro should skip deletion if Home is already missing and continue running the remaining code.
The error-handling block temporarily ignores the expected error from looking for a missing worksheet, then restores normal error handling before the deletion occurs. Display alerts are disabled only during the deletion so Excel does not show a confirmation prompt.
In Excel, press Alt+F11, locate the workbook in the Project Explorer, and open the module that contains the recorded macro.
At the beginning of the procedure, add: Dim ws As Worksheet
Before the deletion line, add: On Error Resume Next, Set ws = ThisWorkbook.Worksheets("Home"), and On Error GoTo 0.
Replace the direct deletion code with: If Not ws Is Nothing Then, Application.DisplayAlerts = False, ws.Delete, Application.DisplayAlerts = True, and End If. Place the remaining macro instructions after End If.
Return to Excel, press Alt+F8, select the macro, and click Run once with Home present and again after Home has already been deleted.
Keep the deletion step separate from the rest of the macro
Use this structure when you want the worksheet-removal logic to be easier to maintain or reuse in other recorded macros.
Edit and test worksheet-deletion macros in WPS Office Spreadsheet
Use WPS Office Spreadsheet to open a compatible workbook, edit the macro procedure, add the worksheet existence check, and test the deletion workflow. VBA availability and support can vary by WPS version, so verify the macro in your installed edition before distributing the file.
- 1. Open the workbook: Launch WPS Office Spreadsheet and use File > Open to load the compatible workbook containing the macro.
- 2. Open the macro editor: Use the workbook's macro or Visual Basic editing entry point, when available in your WPS edition, and open the procedure that deletes Home.
- 3. Add the existence check: Insert the On Error Resume Next lookup for ThisWorkbook.Worksheets("Home"), restore normal error handling, and wrap the deletion in If Not ws Is Nothing Then.
- 4. Test both worksheet states: Run the macro with Home present and with Home already absent, then confirm that the remaining instructions execute in both cases.

Frequently Asked Questions
Why should the macro check whether the Home worksheet exists first?
A worksheet that has already been deleted cannot be retrieved by name. Checking Set ws = ThisWorkbook.Worksheets("Home") and testing If Not ws Is Nothing prevents the missing-sheet error and lets the macro continue.
Where should the remaining macro code go?
Place the remaining instructions after the closing End If statement. That way, the code runs whether Home was found and deleted or was already missing.
Why use ThisWorkbook instead of ActiveWorkbook?
ThisWorkbook refers to the workbook that contains the VBA code. ActiveWorkbook can refer to a different workbook if the user switches files while the macro runs.
What does Application.DisplayAlerts = False do during deletion?
It suppresses Excel's confirmation prompt when the worksheet is deleted. Set it back to True immediately after the deletion so later workbook actions continue to show normal alerts.
Can the macro delete Home if it is the only visible worksheet?
No. Excel requires at least one visible worksheet. Add or unhide another worksheet before running the deletion code, or handle that condition separately in the macro.




