ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Determining if a worksheet exists within a workbook (https://www.excelbanter.com/excel-programming/271843-determining-if-worksheet-exists-within-workbook.html)

Cory Schneider

Determining if a worksheet exists within a workbook
 
I have a macro that creates a new excel workbook and adds a variable
number of new worksheets to the new workbook.

I would like to delete any excess worksheets from this newly created
workbook (ie: Default "Sheet1", "Sheet2", "Sheet3"). I can add code
to delete these sheets:

Sheets("Sheet1").Delete
Sheets("Sheet2").Delete etc...

but, the number of sheets added to a new excel workbook will vary
depending on the individual user's Excel defaults.

1. Is there a way to determine if a sheet exists, and if it does,
delete it, ie:

If Sheets("Sheet1").Exists then Delete Sheets("Sheet1").

2. Is there a way to within a macro force a new workbook to open with
a set number of sheets?

Any help would be appreciated!

Thanks,
Cory

Alain CROS.

Determining if a worksheet exists within a workbook
 
Hi.

Sub AddWkbOneSheet()
Workbooks.Add xlWBATWorksheet
End Sub

Sub AddWkbFiveSheets()
Dim Exnb As Long
Exnb = Application.SheetsInNewWorkbook
Application.SheetsInNewWorkbook = 5
Workbooks.Add
Application.SheetsInNewWorkbook = Exnb
End Sub

Alain CROS.

-----Original Message-----
I have a macro that creates a new excel workbook and adds

a variable
number of new worksheets to the new workbook.

I would like to delete any excess worksheets from this

newly created
workbook (ie: Default "Sheet1", "Sheet2", "Sheet3"). I

can add code
to delete these sheets:

Sheets("Sheet1").Delete
Sheets("Sheet2").Delete etc...

but, the number of sheets added to a new excel workbook

will vary
depending on the individual user's Excel defaults.

1. Is there a way to determine if a sheet exists, and if

it does,
delete it, ie:

If Sheets("Sheet1").Exists then Delete Sheets("Sheet1").

2. Is there a way to within a macro force a new workbook

to open with
a set number of sheets?

Any help would be appreciated!

Thanks,
Cory
.



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

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