Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3
Default code to delete sheets not in use based on a cell's drop down list

Hi
I have a cell with a drop down box containing the list of all of the
worksheets in my workbook. When a worksheet name from this list is selected,
I would like to write some code that will delete all of the other worksheets
on closing. I have no idea where to start, and am desperate for some help.
Please, please help. Nat
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 638
Default code to delete sheets not in use based on a cell's drop down list

On Sep 19, 7:14 pm, kangasnat
wrote:
Hi
I have a cell with a drop down box containing the list of all of the
worksheets in my workbook. When a worksheet name from this list is selected,
I would like to write some code that will delete all of the other worksheets
on closing. I have no idea where to start, and am desperate for some help.
Please, please help. Nat


So, you don't want the sheet deletion process to happen until the time
of closing, correct? If so, use the Workbook_BeforeClose event of the
ThisWorkbook module. Change sheet names and cells references
accordingly. Also, keep in mind that there is no error handling in
this, so you would need to add that. HTH

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wsDropDown As Worksheet, wsKeepSheet As Worksheet
Dim wks As Worksheet
'sheet containing the dropdown
Set wsDropDown = Sheets("DropDown")
'sheet selected from dropdown (change to cell containing dropdown)
Set wsKeepSheet = Sheets(wsDropDown.Range("A1").Text)
For Each wks In ActiveWorkbook.Worksheets
If wks.Name < wsDropDown.Name And _
wks.Name < wsKeepSheet.Name Then
Application.DisplayAlerts = False
wks.Delete
Application.DisplayAlerts = True
End If
Next wks
Set wsDropDown = Nothing
Set wsKeepSheet = Nothing
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3
Default code to delete sheets not in use based on a cell's drop down l

Thank you for your very prompt response. I understand now!
What a legend!
Nat

"JW" wrote:

On Sep 19, 7:14 pm, kangasnat
wrote:
Hi
I have a cell with a drop down box containing the list of all of the
worksheets in my workbook. When a worksheet name from this list is selected,
I would like to write some code that will delete all of the other worksheets
on closing. I have no idea where to start, and am desperate for some help.
Please, please help. Nat


So, you don't want the sheet deletion process to happen until the time
of closing, correct? If so, use the Workbook_BeforeClose event of the
ThisWorkbook module. Change sheet names and cells references
accordingly. Also, keep in mind that there is no error handling in
this, so you would need to add that. HTH

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wsDropDown As Worksheet, wsKeepSheet As Worksheet
Dim wks As Worksheet
'sheet containing the dropdown
Set wsDropDown = Sheets("DropDown")
'sheet selected from dropdown (change to cell containing dropdown)
Set wsKeepSheet = Sheets(wsDropDown.Range("A1").Text)
For Each wks In ActiveWorkbook.Worksheets
If wks.Name < wsDropDown.Name And _
wks.Name < wsKeepSheet.Name Then
Application.DisplayAlerts = False
wks.Delete
Application.DisplayAlerts = True
End If
Next wks
Set wsDropDown = Nothing
Set wsKeepSheet = Nothing
End Sub


Reply
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
Code help, delete rows based on column criteria Stout Excel Discussion (Misc queries) 2 March 20th 07 01:17 PM
delete used records from drop down list? Prashant T Excel Worksheet Functions 3 August 4th 06 09:56 PM
Code to an in cell drop down list frendabrenda1 Excel Discussion (Misc queries) 4 September 1st 05 08:06 PM
How to add and delete items from a list box or drop down box? Fifee Excel Discussion (Misc queries) 3 August 12th 05 01:49 PM
How do I color code items in a drop down list? Beckers1986 Excel Discussion (Misc queries) 1 January 23rd 05 02:02 PM


All times are GMT +1. The time now is 04:33 AM.

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

About Us

"It's about Microsoft Excel"