VBA & Macro Problems

Delete an Excel Worksheet Selected in a UserForm ComboBox Using VBA

Anum MushtaqAnum Mushtaq Nov 13, 2026 868 views

Question details

Delete the Excel worksheet whose name is selected in a UserForm ComboBox, with checks for invalid selections and deletion restrictions.

Delete an Excel Worksheet Selected in a UserForm ComboBox Using VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A UserForm contains a ComboBox listing worksheet names and a command button that should delete the selected worksheet.
Observed behavior
The procedure needs to pass the selected worksheet name correctly, protect required worksheets, handle deletion warnings, and resolve a reported compile error: Label not defined.
Before you start

Open the Visual Basic Editor and verify the ComboBox and command button (Name) properties in the UserForm Properties window. Confirm that the macro is stored in the workbook you intend to modify, because ThisWorkbook targets the workbook containing the code.

Solution 1Recommended

Validate the ComboBox selection and delete the worksheet safely

Use a command button procedure that reads the selected sheet name, checks workbook restrictions, and restores Excel alerts even if deletion fails.

Deleting a worksheet through VBA cannot be undone with Excel's Undo command. The example asks for confirmation before suppressing Excel's built-in deletion warning.

Workbook structure protection blocks worksheet deletion. Worksheet protection alone is a different setting and does not necessarily prevent deleting the sheet.

1
Check the control names

In the Visual Basic Editor, open the UserForm designer. Select the ComboBox and check its (Name) property, then repeat for the delete button. This example uses ComboBox1 and CommandButton1; replace those names if your controls differ.

2
Add the command button code

Double-click the delete button in the UserForm designer and replace its Click procedure with the following code. Keep the event procedure name aligned with the button's actual name. Private Sub CommandButton1_Click() Dim sheetName As String Dim target As Worksheet Dim ws As Worksheet Dim visibleCount As Long Dim previousAlerts As Boolean Dim errorText As String previousAlerts = Application.DisplayAlerts On Error GoTo DeleteFailed sheetName = CStr(Me.ComboBox1.Value) If Len(Trim$(sheetName)) = 0 Then MsgBox "Select a worksheet first.", vbInformation Exit Sub End If Select Case LCase$(sheetName) Case "menu", "ward_lab_generic" MsgBox "This required worksheet cannot be deleted.", vbExclamation Exit Sub End Select On Error Resume Next Set target = ThisWorkbook.Worksheets(sheetName) On Error GoTo DeleteFailed If target Is Nothing Then MsgBox "The selected worksheet was not found in this workbook.", vbExclamation Exit Sub End If If ThisWorkbook.ProtectStructure Then MsgBox "Unprotect the workbook structure before deleting a worksheet.", vbExclamation Exit Sub End If For Each ws In ThisWorkbook.Worksheets If ws.Visible = xlSheetVisible Then visibleCount = visibleCount + 1 End If Next ws If target.Visible = xlSheetVisible And visibleCount <= 1 Then MsgBox "Keep at least one visible worksheet.", vbExclamation Exit Sub End If If MsgBox("Permanently delete worksheet '" & sheetName & "'?", _ vbYesNo + vbExclamation, "Delete worksheet") <> vbYes Then Exit Sub End If Application.DisplayAlerts = False target.Delete Application.DisplayAlerts = previousAlerts Me.ComboBox1.Value = vbNullString MsgBox "Worksheet deleted.", vbInformation Exit Sub DeleteFailed: errorText = Err.Description Application.DisplayAlerts = previousAlerts MsgBox "The operation could not be completed: " & errorText, vbExclamation End Sub

3
Compile and test the procedure

Choose Debug > Compile VBAProject in the Visual Basic Editor. Run the UserForm against a disposable test worksheet, select its name in ComboBox1, and click the delete button. Also test an empty selection and the protected names Menu and Ward_Lab_Generic.

4
Refresh the worksheet list

After a successful deletion, rebuild the ComboBox list using your existing worksheet-list routine, or close and reopen the UserForm if its Initialize procedure populates the list. Clearing the selected value alone does not remove the deleted sheet's name from the list.

Validate the ComboBox selection and delete the worksheet safely
Keep the workbook reference explicit: ThisWorkbook is appropriate only when the UserForm code belongs to the target workbook. If the code is in PERSONAL.XLSB or an add-in, pass an explicit target Workbook reference instead of blindly substituting ActiveWorkbook.
Free Microsoft Office alternative

Use WPS Spreadsheet for everyday Excel workbook editing

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Excel formats, making everyday workbook migration straightforward. It does not repair Excel VBA compile errors; support for running this UserForm macro depends on the WPS edition and environment.

Open and edit Microsoft Excel .xlsx and .xls files for everyday spreadsheet work.Manage worksheet tabs manually without building a VBA UserForm.Use a familiar spreadsheet interface with common workbook-editing tools.Move everyday Excel-format files into a lightweight Office alternative, while validating any macro-dependent workflow separately.
microsoft office alternative - wps office

Frequently Asked Questions

Why does ThisWorkbook.Worksheets(sheetName) fail when the sheet appears in my ComboBox?

ThisWorkbook refers to the workbook containing the VBA code, not necessarily the workbook currently on screen. Check where the UserForm is stored and verify that the ComboBox value exactly matches a worksheet name in that workbook. A stale ComboBox list can also contain a previously deleted name.

Do I need to unprotect a worksheet before deleting it?

Worksheet protection and workbook structure protection are different. Check ThisWorkbook.ProtectStructure; if it is True, use Excel's Review > Protect Workbook control to remove structure protection with the required password before deleting the sheet.

How can I avoid leaving Excel deletion warnings disabled?

Save the existing Application.DisplayAlerts value before changing it. Restore that value immediately after deletion and in the error handler. Do not use Application.DisplayAlerts = False without a cleanup path.

Can I stop users from deleting Menu and Ward_Lab_Generic?

Yes. In the command button's Click procedure, check the selected name before calling Delete. The example uses Select Case LCase$(sheetName) to block both names regardless of capitalization. You can also exclude those sheets when populating the ComboBox.

Can this VBA UserForm run unchanged in WPS Spreadsheet?

Do not assume it will. VBA and UserForm support varies by WPS edition and environment. Verify macro support in your installed version and test the form on a disposable workbook; use Microsoft Excel for this procedure if the required features are unavailable.