ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA code, delete worksheets (https://www.excelbanter.com/excel-programming/402636-vba-code-delete-worksheets.html)

Robert[_30_]

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

Don Guillett

VBA code, delete worksheets
 
Sub deleteshts()
Application.DisplayAlerts = False
For i = 3 To Sheets.Count
Sheets(i).Delete
Next
Application.DisplayAlerts = True
End Sub
--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"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



joel

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


Bob Phillips

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





All times are GMT +1. The time now is 11:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com