Home |
Search |
Today's Posts |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Slightly updated version of Tim's code in case they try and delete all
sheets gives a warning and exits Option Explicit Private Sub UserForm_Initialize() Call RefreshList CommandButton1.Caption = "Delete Selection" ListBox1.MultiSelect = fmMultiSelectExtended End Sub Sub RefreshList() ListBox1.Clear Dim wks As Worksheet For Each wks In ThisWorkbook.Worksheets ListBox1.AddItem wks.Name Next End Sub Private Sub CommandButton1_Click() Dim i As Integer Dim fAllSelected As Boolean fAllSelected = True For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) = False Then Exit For End If Next i If fAllSelected Then MsgBox "You cannot delete all of the sheets", vbExclamation Exit Sub End If If MsgBox("Warning. This will remove the selected sheets. Continue?", _ vbYesNo) = vbNo Then Exit Sub For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) = True Then Application.DisplayAlerts = False ThisWorkbook.Worksheets(ListBox1.List(i)).Delete Application.DisplayAlerts = True End If Next i Call RefreshList End Sub -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Tim Zych" wrote in message ... Correction. Use this one instead. Last one's message was misplaced. Private Sub UserForm_Initialize() Call RefreshList CommandButton1.Caption = "Delete Selection" ListBox1.MultiSelect = fmMultiSelectExtended End Sub Sub RefreshList() ListBox1.Clear Dim wks As Worksheet For Each wks In ThisWorkbook.Worksheets ListBox1.AddItem wks.Name Next End Sub Private Sub CommandButton1_Click() If MsgBox("Warning. This will remove the selected sheets. Continue?", _ vbYesNo) = vbNo Then Exit Sub Dim i As Integer For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) = True Then Application.DisplayAlerts = False ThisWorkbook.Worksheets(ListBox1.List(i)).Delete Application.DisplayAlerts = True End If Next i Call RefreshList End Sub "Tim Zych" wrote in message ... Here's a macro that displays a list of the worksheets in the workbook. Select the sheets, click the button and they will be removed. Deleting sheets is not a reversible action so save a backup workbook. Create a userform with a listbox and command button, UserForm1, ListBox1, CommandButton1. Code goes in the form. Private Sub UserForm_Initialize() Call RefreshList CommandButton1.Caption = "Delete Selection" ListBox1.MultiSelect = fmMultiSelectExtended End Sub Sub RefreshList() ListBox1.Clear Dim wks As Worksheet For Each wks In ThisWorkbook.Worksheets ListBox1.AddItem wks.Name Next End Sub Private Sub CommandButton1_Click() Dim i As Integer For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) = True Then If MsgBox("Warning. This will remove the selected sheets. Continue?", vbYesNo) = _ vbYes Then Application.DisplayAlerts = False ThisWorkbook.Worksheets(ListBox1.List(i)).Delete Application.DisplayAlerts = True End If End If Next i Call RefreshList End Sub "peterG" wrote in message ... I want to be able to delete all sheets from a workbook except a few specific ones. Is there any way to get a list of current sheets in a form where I can delete the names I want to keep and select the rest? TIA peterG |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Selecting sheets - II | Excel Discussion (Misc queries) | |||
Selecting sheets | Excel Discussion (Misc queries) | |||
selecting sheets | Excel Programming | |||
Changing the value in multiple sheets without selecting those sheets | Excel Programming | |||
selecting sheets | Excel Programming |