Delete an Excel Worksheet Selected in a UserForm ComboBox Using VBA
Question details
Delete the Excel worksheet whose name is selected in a UserForm ComboBox, with checks for invalid selections and deletion restrictions.

- 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.
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.
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.
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.
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
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.
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.

Fix the VBA compile error “Label not defined”
Use this solution when VBA cannot compile a GoTo or error-handling statement because its destination label is missing.
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.

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.




