View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tim Zych[_4_] Tim Zych[_4_] is offline
external usenet poster
 
Posts: 58
Default selecting sheets in vb

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