Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default VBA code, delete worksheets

Hello,

Could someone advice on the VBA statement that allows me to delete all
worksheets within my workbook except for Sheet1 and Sheet2.

Example: Mij Workbook contains (for example) the following worksheets:
Sheet1, Sheet2,Sheet3,Sheet4, Sheet5
Before further steps are taken I would like to delete all sheets
except Sheet1 and Sheet2. As the deleted sheets might vary in name and
number it is prefered to define a statement that deletes all sheets
except for Sheet1 and Sheet2. I tried to create a statement using the
recorder but in this occasion all actual names are recorded.

many thanks!!!!

Best regards,
Robert
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA code, delete worksheets

A pop up will come up asking you to confirm that you really want to delete.
here is the code

Sub deletesheets()

For Each sh In ThisWorkbook.Sheets
If sh.Name < "Sheet1" And _
sh.Name < "Sheet2" Then

sh.Delete
End If
Next sh

End Sub


"Robert" wrote:

Hello,

Could someone advice on the VBA statement that allows me to delete all
worksheets within my workbook except for Sheet1 and Sheet2.

Example: Mij Workbook contains (for example) the following worksheets:
Sheet1, Sheet2,Sheet3,Sheet4, Sheet5
Before further steps are taken I would like to delete all sheets
except Sheet1 and Sheet2. As the deleted sheets might vary in name and
number it is prefered to define a statement that deletes all sheets
except for Sheet1 and Sheet2. I tried to create a statement using the
recorder but in this occasion all actual names are recorded.

many thanks!!!!

Best regards,
Robert

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default VBA code, delete worksheets

Public Sub DeleteSheets()

Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Sheets

If sh.Name < "Sheet1" And _
sh.Name < "Sheet2" Then

sh.Delete
End If
Next sh
Application.DisplayAlerts = True

End Sub


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Robert" wrote in message
...
Hello,

Could someone advice on the VBA statement that allows me to delete all
worksheets within my workbook except for Sheet1 and Sheet2.

Example: Mij Workbook contains (for example) the following worksheets:
Sheet1, Sheet2,Sheet3,Sheet4, Sheet5
Before further steps are taken I would like to delete all sheets
except Sheet1 and Sheet2. As the deleted sheets might vary in name and
number it is prefered to define a statement that deletes all sheets
except for Sheet1 and Sheet2. I tried to create a statement using the
recorder but in this occasion all actual names are recorded.

many thanks!!!!

Best regards,
Robert



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
Delete Blank Rows Code - Multiple Worksheets - Multiple Documents BenS Excel Discussion (Misc queries) 3 June 29th 07 12:20 AM
Delete worksheets and the code sheets remain? Mike[_106_] Excel Programming 2 April 2nd 06 10:43 PM
Array Code to Delete Worksheets Volsfan Excel Programming 3 September 14th 05 04:42 PM
VBA code delete code but ask for password and unlock VBA protection WashoeJeff Excel Programming 0 January 27th 04 07:07 AM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM


All times are GMT +1. The time now is 12:10 PM.

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"