Dynamic Userform to delete selected sheets

bujaman

Board Regular
Joined
Apr 2, 2009
Messages
56
I want to create a userform that will list all worksheets in a workbook with CheckBoxes next to each one. The user then can check the worksheets they wish to delete, and click the OK button to delete them. I have been able to create a userform that lists all the sheets, but that is about it. Any help would be awesome! Thanks!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe something like this...

1) Place a listbox on the userform.

2) In the Properties Window for the listbox, set the MultiSelect property to '1-fmMultiSelectMulti'.

3) In the Properties Window for the listbox, set the ListStyle property to '1-fmListStyleOption'.

4) Place the following code in the userform module (right-click the userform, and select 'View Code')...

Code:
Option Explicit

Private Sub CommandButton1_Click()
    Dim MyArray() As Variant
    Dim i As Long
    Dim Cnt As Long
    With Me.ListBox1
        Cnt = 0
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                Cnt = Cnt + 1
                ReDim Preserve MyArray(1 To Cnt)
                MyArray(Cnt) = .List(i)
            End If
        Next i
        If Cnt > 0 Then
            If Worksheets.Count > UBound(MyArray) Then
                Application.DisplayAlerts = False
                Worksheets(MyArray).Delete
                Application.DisplayAlerts = True
                Call UpdateSheetList
            Else
                MsgBox "A workbook must contain at least one visible sheet.", vbExclamation
            End If
        Else
            MsgBox "Please select one or more sheets for deletion...", vbExclamation
        End If
    End With
End Sub

Private Sub UserForm_Initialize()
    Call UpdateSheetList
End Sub

Private Sub UpdateSheetList()
    Dim wks As Worksheet
    With Me.ListBox1
        .Clear
        For Each wks In Worksheets
            .AddItem wks.Name
        Next wks
    End With
End Sub

Change the code names for the controls, accordingly. Also, note that the user can select more than one sheet for deletion.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,869
Members
449,054
Latest member
juliecooper255

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top