VBA & Macro Problems

Confirm Before Deleting a UserForm ListBox Row in Excel VBA

Steven ParkerSteven Parker Nov 18, 2026 869 views

Question details

Add a confirmation prompt before a Delete button removes the selected row from an Excel UserForm ListBox.

Confirm Before Deleting a UserForm ListBox Row in Excel VBA
Product
Microsoft Excel VBA UserForm
Device & OS
not provided
Scenario
A UserForm has a ListBox and a Delete button, and the user wants to retain the existing row-selection behavior.
Observed behavior
The Delete button removes a selected ListBox row without a confirmation prompt.
Before you start

Open the UserForm in the VBA editor and check the names of the ListBox and Delete button. Confirm whether the ListBox uses RowSource, because a bound ListBox cannot be changed directly with RemoveItem.

Solution 1Recommended

Add confirmation to the Delete button’s VBA event

Use this approach for a single-selection ListBox that is not bound through RowSource.

The code removes an item from the ListBox only; it does not delete a row from the worksheet. It leaves the ListBox selection settings unchanged.

1
Open the button event

In Excel, press Alt+F11 to open the VBA editor. Open the UserForm and double-click its Delete button to open the Click event.

2
Add the confirmation logic

Adapt the following code to your existing Click event. Replace DeleteButton and ListBox1 with the actual control names, and keep any other necessary selection-related code in the event: Private Sub DeleteButton_Click() Dim selectedRow As Long selectedRow = Me.ListBox1.ListIndex If selectedRow = -1 Then MsgBox "Select a row to delete.", vbInformation Exit Sub End If If MsgBox("Delete the selected row?", vbYesNo + vbQuestion, "Confirm deletion") = vbYes Then Me.ListBox1.RemoveItem selectedRow End If End Sub

3
Test both choices

Run the UserForm, select a ListBox row, and click Delete. Choose No to keep the row, then repeat and choose Yes to remove it. Also test the button with no row selected.

Add confirmation to the Delete button’s VBA event
RowSource-bound lists: If the ListBox uses RowSource, RemoveItem will not work. Instead, identify the corresponding source data row, delete it only after confirmation, and refresh the ListBox binding. Verify the mapping before deleting worksheet data.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for Microsoft Excel formats, making it easy to move everyday spreadsheet work. Excel VBA UserForms and macros may not be fully compatible, so test them before switching.

Open and edit Microsoft Excel XLSX and XLS filesWork with spreadsheet lists in a familiar interfaceUse a free, lightweight office suiteMove everyday spreadsheet work between compatible formats
microsoft office alternative - wps office

Frequently Asked Questions

How do I prevent deletion when no ListBox row is selected?

In the Delete button’s Click event, check whether ListBox1.ListIndex equals -1. Show a message and use Exit Sub before displaying the confirmation prompt.

Will RemoveItem delete the corresponding worksheet row?

No. ListBox1.RemoveItem removes the item from the ListBox. To delete worksheet data, separately identify and delete the corresponding source row after the user confirms.

Why does RemoveItem fail for my ListBox?

Check the ListBox’s RowSource property in the UserForm editor. If it is bound to a range, update the source data instead of calling RemoveItem, then refresh the binding.

Where should I put the confirmation code?

In the VBA editor, open the UserForm and double-click the Delete button. Add the confirmation check to its Click event before the existing deletion action.

How do I keep the VBA code in the workbook?

In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). Enable macros only when you trust the workbook.