LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default selecting sheets in vb

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting sheets - II Dr.Schwartz Excel Discussion (Misc queries) 4 July 25th 05 02:31 PM
Selecting sheets Dr.Schwartz Excel Discussion (Misc queries) 4 July 14th 05 01:49 PM
selecting sheets kevin Excel Programming 4 November 27th 03 11:06 PM
Changing the value in multiple sheets without selecting those sheets herm Excel Programming 3 October 14th 03 03:50 PM
selecting sheets jacqui[_2_] Excel Programming 0 September 11th 03 02:46 PM


All times are GMT +1. The time now is 11:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"