Monday, May 16, 2022

How to create new workbook with specific template Excel Vba

To create Workbook with Excel VBA, first thing we must specify which template we need to create such as Workbook with blank sheet, Chart, Macro, International Macro or Dialog. Each template represent by number or special command as below:

Workbook with Blank worksheet

Workbooks.Add(1)
    OR
Workbooks.Add(xlWBATWorksheet)

Workbook with Chart Worksheet

Workbooks.Add(2)
    OR
Workbooks.Add(xlWBATChart)

Workbook with Macro Worksheet

Workbooks.Add(3)
    OR
Workbooks.Add(xlWBATExcel4MacroSheet)

Workbook with International Macro Worksheet

Workbooks.Add(4)
    OR
Workbooks.Add(xlWBATExcel4IntlMacroSheet)

Base on my testing if we use Workbooks.Add(7) it will create Workbook with Dialog Worksheet. If we use number 5 or 6 the result is the same as Workbooks.Add(1) which Blank worksheet. If the number is greater than 7 "Run Time Error 1004 , Method Add of Object Workbooks Failed".

Below completed code to create Workbook with Blank Worksheet:

Option Explicit
Sub CreateWorkbook()
    
    'Variables Declaration
    Dim MstWB As Workbook
    
    'Change the number according to template
    Set MstWB = Workbooks.Add(1)
    
End Sub

Thank You

Microsoft Reference (Workbooks)
Microsoft Reference (Workbooks.Add)
Microsoft Reference (xlwbatemplate)

No comments:

Post a Comment