UserForm showing list of all sheets, ability to delete sheets

noslenwerd

Board Regular
Joined
Nov 12, 2019
Messages
85
Office Version
  1. 365
Hello,

I am hoping to find a way to create a userform, that lists all sheets within a workbook, and somehow gives the user the opportunity to select which sheets they would like to delete from that list.

Is this possible?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Create a listbox on the userform:
Place the below code in the Userform.
VBA Code:
Private Sub UserForm_Initialize()
'Modified  3/3/2021  1:20:52 AM  EDT
ListBox1.Clear
Dim i As Long
For i = 1 To Sheets.Count
    ListBox1.AddItem Sheets(i).Name
Next
End Sub

Place this script in the listbox:
Script runs when you click on a sheet name in the listbox:
VBA Code:
Private Sub ListBox1_Click()
'Modified  3/3/2021  1:24:21 AM  EDT
Sheets(ListBox1.Value).Delete
ListBox1.Clear
Dim i As Long
For i = 1 To Sheets.Count
ListBox1.AddItem Sheets(i).Name
Next
End Sub
 
Upvote 0
@MY answer is this; just a bit concerned that the user could accidently click an item and the sheet is gone. might be safer to choose then click a commandbutton? or right click for context menu.. just something a bit more deliberate in nature from the user
 
Upvote 0
Thank you both for the reply.

@diddi can you give me direction on how that might be done with a userform? I am pretty new to using userforms. Thanks!
 
Upvote 0
just draw a commandbutton on your userform with a caption like "Delete sheet". when the use clicks the commandbutton, the code runs.
so in the code you only need to change 1 line
VBA Code:
'Private Sub ListBox1_Click()  ' old line
Private Sub CommandButton1_Click() ' replace with this.  the commandbutton number has to match your commandbutton
    'Modified  3/3/2021  1:24:21 AM  EDT
    Sheets(ListBox1.Value).Delete
    ListBox1.Clear
    Dim i As Long
    For i = 1 To Sheets.Count
        ListBox1.AddItem Sheets(i).Name
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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