views:

102

answers:

2

Creating a new Excel workbook as in:

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Set xl = New Excel.Application
xl.Visible = False
Set wb = xl.Workbooks.Add

Is there an easy way to stop Excel automatically creating Sheet1, Sheet2, Sheet3?

I can always delete these unwanted sheets afterwards but that feels like a clunky solution.

+4  A: 
xl.SheetsInNewWorkbook = 1

More Information on MSDN (Scroll down to Add method as it applies to the Workbooks object.)

Full Code:

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim restoreSheetsInNewWorkbook As Long

Set xl =  New Excel.Application
restoreSheetsInNewWorkbook = xl.SheetsInNewWorkbook
xl.SheetsInNewWorkbook = 1

Set wb = xl.Workbooks.Add
xl.SheetsInNewWorkbook = restoreSheetsInNewWorkbook 'or just set it to 3'
marg
Don't forget that this needs to be restored to its original setting if you do not wish to annoy users.
Remou
so there is no way to start off with no sheets?
hawbsl
@hawbsl, a workbook must have at least one sheet. Don't add any workbooks if you want an _empty_ excel/app. object.
Nick D
If you want to start with a specific kind of sheet you can use one of the ENUM Values that are specified in the MSDN link above as the Template. e.g. `Workbooks.Add xlWBATChart`
marg
Accepted @marg's answer but really it was @NickD's comment which gave me my answer. @NickD if you want to post your comment as an answer i'll accept that instead.
hawbsl
+1  A: 

Or you can:

Excel 2003 Tools>Options>General Tab and change the "Sheets in new workbook" to 1

Excel 2007 Office Button>Excel Options>Popular Section>When creating new workbooks...>Include this many sheets>1

Macness