Excel VBA Create
Excel VBA Create
COURSES
BOOKS
TUTORIALS
If you're automating some of your Excel work by using Visual Basic for Applications, the
ability to create new workbooks can be of immense help. You may want, for
example, do any of the following:
Create a new workbook based on a particular template.
If that's the case, then this Excel VBA Tutorial should help you. In this blog post, I focus
on how you can easily create new Excel workbooks with VBA.
In the first section of this Tutorial, I introduce some of the most relevant VBA
constructs (with a focus on the Workbooks.Add method) that can help you to quickly
craft a macro that creates a new workbook. In the second section, I provide a step-by-
step explanation of 16 practical macro code examples that you can easily adjust
and start using today.
Use the following Table of Contents to navigate to the section that interests you the
most.
Workbooks.Add Method
o Template Parameter Of Workbooks.Add
Other Useful VBA Constructs To Create A New Workbook
o Workbooks.Open Method
o Application.SheetsInNewWorkbook Property
o Sheets.Add, Worksheets.Add, Charts.Add, DialogSheets.Add, Excel4MacroSheets.Add And
Excel4IntlMacroSheets.Add Methods
o Workbook.SaveAs Method
o Copy Method
o Move Method
o Array Function
Macro Code Examples To Create New Excel Workbook With VBA
o Macro Example #1: Excel VBA Create New Workbook With Default Number Of Sheets
o Macro Examples #2 And #3: Excel VBA Create New Workbook Using Template
o Macro Example #4: Excel VBA Create New Workbook With A Chart Sheet
o Macro Examples #5 And #6: Excel VBA Create New Workbook With Several Worksheets
o Macro Example #7: Excel VBA Create New Workbook With A Worksheet And A Chart Sheet
o Macro Example #8: Excel VBA Create New Workbook With Name
o Macro Examples #9, #10 And #11: Excel VBA Copy One Or Several Worksheets To New
Workbook
o Macro Examples #12, #13 And #14: Excel VBA Move One Or Several Worksheets To New
Workbook
o Macro Examples #15 And #16: Excel VBA Copy Range To New Workbook
Conclusion
This Excel VBA Create New Workbook Tutorial is accompanied by an Excel workbook
containing the data and macros I use in the examples below. You can get immediate
free access to this example workbook by subscribing to the Power Spreadsheets
Newsletter.
The newly created workbook becomes the new active workbook. Therefore, the value
that Workbooks.Add returns is a Workbook object representing that newly created
workbook.
expression.Add(Template)
Template: Optional parameter. It allows you to specify how Excel creates the new
workbook.
Workbooks.Add(Template)
You can use this parameter for purposes of specifying certain characteristics of the
newly created workbook. More precisely, Template allows you to determine which of
the following option Excel applies when creating the workbook:
1. Create a new workbook with the default number of blank sheets. This is the default
value of Template. Therefore, Excel does this whenever you omit the parameter.
2. Create a new workbook with a single sheet of a certain type. As I explain below, you
can choose this option by using the xlWBATemplate constants.
3. Use a particular file as a template. You determine the template Excel uses by
specifying the name of the relevant file.
If you want to create a new workbook based on an Excel template, you can use the
Workbooks.Open method and set the Editable parameter to False. I introduce the
Workbooks.Open method further below.
1. Omit the Template Parameter: The newly created workbook contains the default
number of blank sheets. This number of sheets:
2. Appears (and you can modify it from) within the General tab of the Excel Options
dialog.
2. Specify an existing Excel file as a string: The file you specify is used as a template for
the new workbook.
When specifying the template that Excel uses, include the full path of the file.
For purposes of this tutorial, it's enough for you to know how to use the Open method
to create a new workbook based on an Excel template.
FileName.
Editable.
Considering the above, the following is a very basic syntax of the Workbooks.Open
method geared to create a new workbook based on a template:
Editable:=False: Editable parameter set to False. When Editable is False, Excel creates
a new workbook based on the template you specify with the FileName parameter.
I don't cover the other 13 parameters of Workbooks.Open in this blog post. However,
you may find some of them useful when creating a new workbook based on a template.
If you're interested in reading more about these parameters, please refer to my tutorial
about the topic (to which I link to above).
Application.SheetsInNewWorkbook Property
Application.SheetsInNewWorkbook is the VBA property that determines how many
sheets newly created workbooks have. It's the equivalent of the Include this many
sheets setting within the General tab of the Excel Options dialog.
“expression” represents the Excel Application object. Therefore, it's generally possible
to simplify the syntaxas follows:
Application.SheetsInNewWorkbook
You can usually handle item #1 above by ensuring that your VBA Sub
procedures return SheetsInNewWorkbook to the same setting that applied prior to the
macro being executed. Macro example #5 below shows an example of how you can do
this.
Add.
Sheets.Add.
Worksheets.Add.
Charts.Add.
DialogSheets.Add.
Excel4MacroSheets.Add.
Excel4IntlMacroSheets methods.
I explain these methods in the following sections. Let's start with the…
Sheets.Add Method
The Sheets.Add method allows you to create any of the following sheets:
1. Worksheet.
2. Chart sheet.
3. Dialog sheet.
4. Macro sheet.
Before: Optional argument. The (existing) sheet before which you want to add the newly
created sheet.
After: Optional parameter. The (existing) sheet after which you want the new sheet to be
inserted.
Count: Optional parameter. Allows you to specify the number of sheets that VBA adds.
The default value of Count (applies if you omit the argument) is 1.
Type: Optional parameter. You use Type to specify the type of sheet that Excel adds.
You can either (i)specify the path and name of the template you want to use, or (ii) use
the following values from within the xlSheetType enumeration:
xlWorksheet is the default value. Therefore, this is the type of sheet that Excel adds
if you omit the Type parameter.
The default location of the newly created sheet is before the active sheet.
Therefore, this applies if you omit both the Before and After parameters above.
Considering the comments above, I simplify the syntax of the Add method as follows:
Worksheets.Add.
Charts.Add.
DialogSheets.Add.
Excel4MacroSheets.Add.
Excel4IntlMacroSheets.Add.
The main difference between all of these methods, is the collection they work with.
More precisely:
Sheets.Add works with the Sheets object. This is the collection of all sheets within the
relevant workbook.
Worksheets.Add works with the Worksheets object. That is, the collection of all
worksheets within the appropriate workbook.
Charts.Add makes reference to the Charts collection. This is the collection of all chart
sheets.
The basic syntax of all the methods is substantially the same as the one I describe
above for Sheets.Add. In other words, as follows:
Worksheets.Add:
Charts.Add:
DialogSheets.Add:
DialogSheets.Add(Before, After, Count, Type)
Excel4MacroSheets.Add:
Excel4IntlMacroSheets.Add:
From a broad perspective, most of the parameters of the Add method act in a
similar manner regardless of the object you refer to. Therefore, the comments I make
above regarding the Before, After and Count arguments of the Sheets.Add method also
apply to this section.
When it comes to the Type parameter, there are some differences. Keep in mind, in
particular, the following:
Worksheets.Add: The default value of Type is xlWorksheet. The method fails if you set
Type to xlChart or xlDialogSheet.
Charts.Add: In this case, the default (and only acceptable) value is xlChart. In other
words, the method fails with xlWorksheet, xlExcel4MacroSheet, xlExcel4IntlMacroSheet
and xlDialogsheet.
DialogSheets.Add: The default value is xlDialogSheet. This is also the only acceptable
value. The method doesn't work with xlWorksheet, xlChart, xlExcel4MacroSheet or
xlExcel4IntlMacroSheet.
Workbook.SaveAs Method
From a general perspective, the Workbooks.SaveAs method allows you to save a
workbook file.
As I explain in this tutorial, you can use the SaveAs method to create a new
workbook with a particular filename.
For purposes of this blog post, I use the following simplified syntax of
Workbook.SaveAs:
Workbook.SaveAs Filename:=workbookName
For our purposes, “workbookName” is the name of the workbook you're creating.
The SaveAs method has 12 different parameters. Filename is only one of them. Other
useful parameters that you can use when creating a new workbook with VBA include
the following:
Please read my blog post about this topic (I link to the Archives above) to learn more
about these other arguments.
Copy Method
Further above, I introduce the following versions of the Add method:
Sheets.Add.
Worksheets.Add.
Charts.Add.
DialogSheets.Add.
Excel4MacroSheets.Add.
Excel4IntlMacroSheets.Add.
The same thing occurs with the Copy method. For purposes of this VBA tutorial, I focus
on the Copy method applied to the following objects:
Excel 4.0 macro sheet (Excel4MacroSheet.Copy) or Excel 4.0 macro sheets collection
(Excel4MacroSheets.Copy).
expression.Copy(Before, After)
After: Optional argument. The sheet after which you want to copy the sheet(s).
Therefore, I simplify the syntax as follows, depending on whether the macro works with
the whole collection (Sheets) or not (Sheet):
Sheet.Copy(Before, After)
Sheets.Copy(Before, After)
Before and After are mutually exclusive arguments. In other words, you can only use
one or the other:
You can omit both Before and After. The consequence of doing this is particularly
interesting in the context of creating a new workbook (the topic of this post):
Sheet.Copy
Sheets.Copy
Worksheet.Copy or Worksheets.Copy.
Chart.Copy or Charts.Copy.
DialogSheet.Copy or DialogSheets.Copy.
Excel4MacroSheet.Copy or Excel4MacroSheets.Copy.
Excel4IntlMacroSheet.Copy or Excel4intlMacroSheets.Copy.
The basic (simplified) syntax of the methods I cover in this section is as follows:
Worksheet.Copy:
Worksheet.Copy(Before, After)
Worksheets.Copy:
Worksheets.Copy(Before, After)
Chart.Copy:
Chart.Copy(Before, After)
Charts.Copy:
Charts.Copy(Before, After)
DialogSheet.Copy:
DialogSheet.Copy(Before, After)
DialogSheets.Copy:
DialogSheets.Copy(Before, After)
Excel4MacroSheet.Copy:
Excel4MacroSheet.Copy(Before, After)
Excel4MacroSheets.Copy:
Excel4MacroSheets.Copy(Before, After)
Excel4IntlMacroSheet.Copy:
Excel4IntlMacroSheets.Copy:
Excel4IntlMacroSheets.Copy(Before, After)
Move Method
The Move method is, to a certain extent, similar to the Copy method I describe in the
previous section.
When you manually copy or move a sheet, you use the Move or Copy dialog box. This
dialog box allows you to specify whether you want to create a copy or not.
If you choose to create a copy, you're using the equivalent of the Copy method.
Otherwise, you're working with the equivalent of the Move method.
Similar to the Copy method, you can apply the Move to the following objects:
Excel 4.0 macro sheet (Excel4MacroSheet.Move) or Excel 4.0 macro sheets collection
(Excel4MacroSheets.Move).
expression.Move(Before, After)
Before: Optional parameter. Sheet before which you want to place the moved sheet(s).
After: Optional argument. Sheet after which the moved sheet(s) is(are) located.
Considering the above, I simplify the syntax as follows, depending on whether the
macro works with the whole Sheets collection (Sheets.Move) or not (Sheet.Move):
Sheet.Move(Before, After)
Sheets.Move(Before, After)
Similar to what occurs with the Copy method (which I describe above), the following
comments are applicable:
Before and After are mutually exclusive. Therefore, choose to use one or the other (not
both).
If you omit both Before and After, Excel creates a new workbook with the moved
sheet(s). Therefore, in this case, you can further simplify the syntax as follows:
Sheet.Move
Sheets.Move
When applying the Move method to a whole collection, bear in mind that an Excel
workbook generally must keep at least 1 visible sheet. If execution of your macro
results in a workbook not having any visible sheets, you usually get a run-time error.
Worksheet.Move:
Worksheet.Move(Before, After)
Worksheets.Move:
Worksheets.Move(Before, After)
Chart.Move:
Chart.Move(Before, After)
Charts.Move:
Charts.Move(Before, After)
DialogSheet.Move:
DialogSheet.Move(Before, After)
DialogSheets.Move:
DialogSheets.Move(Before, After)
Excel4MacroSheet.Move:
Excel4MacroSheet.Move(Before, After)
Excel4MacroSheets.Move:
Excel4MacroSheets.Move(Before, After)
Excel4IntlMacroSheet.Move:
Excel4IntlMacroSheet.Move(Before, After)
Excel4IntlMacroSheets.Move:
ExcelIntlMacroSheets.Move(Before, After)
Array Function
From a broad perspective, you can define an array by the following characteristics:
The elements are sequentially indexed. Therefore, each element has a unique identifying
index number.
I cover the topic of VBA arrays in detail here. Please refer to that tutorial in order to
learn more about arrays and how they can help you when working with Visual Basic for
Applications.
In the context of this tutorial, arrays help us manipulate several worksheets at once.
More precisely, as I show in examples #10 and #13 below, you can use an array to
copy or move several worksheets to a new workbook.
The examples below work with the Array Function. Array allows you to obtain a Variant
that contains an array. Its basic syntax is as follows:
Array(argumentList)
For these purposes, “argumentList” represents the list of values you want to assign to
the array elements. You separate the different arguments using a comma (,).
Explaining each of these constructs exceeds the scope of this VBA tutorial. However, in
macro examples #15 and #16 below, I provide VBA code examples that use the
Range.Copy method to copy a range to a newly created workbook.
You can use the information and examples I provide in the blog post about copying and
pasting with VBA (follow the link I provide above) for purposes of easily using the other
VBA constructs I mention above.
Workbooks.Add
The following macro example (Create_New_Workbook) shows how you can easily
implement this statement:
Workbooks.Add
This simply makes reference to the Workbooks.Add method. It uses this method for
purposes of adding a new workbook to the Workbooks collection. In other words, the
statement simply creates a new workbook.
Even though each of the other macro examples (below) targets a different situation,
several of them rely on this basic statement (or a close derivative).
When I execute the sample macro above, Excel simply creates a new workbook
with (i) the default name (Book1 in this case), and (ii) the default number of blank
worksheets (1 in this example).
Macro Examples #2 And #3: Excel VBA Create New
Workbook Using Template
The following statement structure allows you to create a new Excel workbook using
an existing file as a template:
Workbooks.Add Template:=”Filename”
For these purposes, “Filename” is the name of the Excel file you're using as template for
the new workbook.
As an alternative to the above, you can create a new workbook based on an Excel
template by working with the Workbooks.Open method. The following basic syntax
allows you to do this:
Within this structure, “TemplateFilename” makes reference to the template you want to
use.
When I execute this Sub procedure, Excel creates a new workbook using Puneet's
useful template:
Workbooks.Add Template:=xlWBATChart
Application.SheetsInNewWorkbook = new#
Workbooks.Add
For these purposes, “new#” is the number of worksheets you want the newly created
workbook to have.
The process followed by this macro to create the new workbook is as follows:
This structure I provide is quite basic. Therefore, it simply modifies the setting that
determines how many worksheets are included in a new workbook.
The following screenshot shows the workbook that Excel creates when I execute this
macro sample. Notice that, as expected, it has 3 sheets.
An alternative to relying on the Application.SheetsInNewWorkbook property is to
work with the Sheets.Add, Worksheets.Add, Charts.Add or DialogSheets.Add
methods that I explain above. These methods have some advantages (including more
flexibility) over relying on the Application.SheetsInNewWorkbook property.
In this case, the basic code syntax you can use to create a new workbook with several
sheets is as follows:
Workbooks.Add
Worksheets.Add Count:=#
For these purposes, “#” is the number of additional sheets you want to add. The
workbook created by Workbooks.Add comes (already) with the number of worksheets
determined by the Application.SheetsInNewWorkbook property (usually 1).
The particular macro structure above works with the Worksheets.Add method. You can,
however, also work with the Sheets.Add, Charts.Add or DialogSheets.Add
methods by using a similar syntax. For purposes of understanding the differences
between each of these methods (and how to use them), please refer to the appropriate
section above.
The following image shows the workbook that Excel creates when I execute this macro.
Notice that, just as in the previous example, the newly created workbook has 3
worksheets.
As I mention above, you can use similar macros to work with the Sheets.Add,
Charts.Add or DialogSheets.Add methods. The following macro sample provides
more ideas of what you can achieve with these methods:
Workbooks.Add
Charts.Add
This macro follows a simple 2-step process to create a new workbook with a worksheet
and a chart sheet:
1. Creates a new workbook.
The newly created workbook looks as follows. Notice that, as expected, the workbook
has 1 worksheets and 1 chart sheet.
Macro Example #8: Excel VBA Create New Workbook
With Name
The Workbooks.Add method by itself creates a new workbook. Generally, the name of
the newly created workbook is automatically determined by Excel. The actual
name varies slightly depending on how you create the new workbook. However, as a
general rule, the filename follows the following structure:
Item#
Usually, “Item” is “Book”. In other words, the newly created workbooks are named
Book1, Book2, Book3, etc. However, there are some exceptions to this general rule. For
example, if you use the Template parameter of the Workbooks.Add method, the default
name of the new workbook is determined on the basis of Template's value as follows:
If Template specifies an Excel file: “Item” is replaced by the name of the Excel file you
specify. As a consequence, new workbooks have the name of Template followed by the
appropriate number (TemplateName1, TemplateName2, TemplateName3, and so on).
Excel applies this same rule if you create a new workbook by using the Workbooks.Open
method with an Excel template. I show a practical example of a macro that does this in
sample #3 above.
Despite the above, you can easily assign a name to the newly created workbook by
saving it. Use the following code syntax to do this:
Workbooks.Add.SaveAs Filename:=workbookName
“workbookName” is the name you want to assign to the newly created workbook.
The process followed by this statement to create a new workbook with name is as
follows:
1. Create a new workbook (Workbooks.Add).
The following screenshot shows the results I get when executing this macro. Notice
that, as expected, the workbook is indeed named “Excel VBA Create New Workbook
With Name”.
Macro Examples #9, #10 And #11: Excel VBA Copy One
Or Several Worksheets To New Workbook
The Copy method allows you to copy sheets to a new workbook. In this section, I
show how you can use this method copy one or several worksheets to a new
workbook.
In the examples below, I work with the Worksheet.Copy method. You can, however,
easily adjust the code in order to work with any of the following:
Sheet.Copy or Sheets.Copy.
Chart.Copy or Charts.Copy.
DialogSheet.Copy or DialogSheets.Copy.
Excel4MacroSheet.Copy or Excel4MacroSheet.Copy.
Excel4IntlMacroSheet.Copy or Excel4IntlMacroSheets.Copy.
The following VBA statement allows you to copy a worksheet to a new workbook. In
this case, the copied worksheet is the only worksheet in the newly created workbook:
Worksheet.Copy
The following image shows the results I obtain when running the macro. Notice that the
newly created workbook, indeed, contains the copied worksheet (Copy1).
If you want to copy several worksheets to a new workbook, the basic statement
structure I explain above continues to apply. In order to get several worksheets at
once, you can use the Array Function. The following statement structure can serve
you as guidance:
For these purposes, “Worksheet1” through “Worksheet#” are the worksheets you want
to copy.
Worksheets.Copy
In this case, the newly created workbook contains all the worksheets within the
applicable workbook.
Macro Examples #12, #13 And #14: Excel VBA Move One
Or Several Worksheets To New Workbook
You can use the logic behind the macros examples that copy worksheets to a new
workbook for purposes of moving worksheets to a new workbook. For these purposes,
you use the Move method.
Similar to the previous examples #9 to #11, the following sample procedures work with
worksheets. However, using the information I provide in the relevant sections
above, you can easily adjust them to work with the following methods:
Sheet.Move or Sheets.Move.
Chart.Move or Charts.Move.
DialogSheet.Move or DialogSheets.Move.
Excel4MacroSheet.Move or Excel4MacroSheet.Move.
Excel4IntlMacroSheet.Move or Excel4IntlMacroSheets.Move.
Let's start:
Use the following statement syntax to move a worksheet to a new workbook. The
moved worksheet is the only within the newly created workbook.
Worksheet.Move
The following screenshot shows the newly created workbook. Notice that the workbook
contains the moved worksheet (Move1).
If you want to move several worksheets to a new workbook use the Array Function
alongside the Move method. The basic structure of the relevant statement is as
follows:
The workbook that Excel creates when I execute this macro looks as follows. Notice
that the workbook contains the 5 worksheets (Move1 to Move5) that I specified in the
code above.
Finally, if you're moving all of the worksheets within a particular workbook, use
Worksheets.Move method. In other words, simply use the following VBA statement:
Worksheets.Move
The following image shows the results I get when executing this macro. All worksheets
in the applicable workbook (Copy1 to Copy5, Move1 to Move5 and Range) are moved
to a new workbook.
The only remaining sheet in the source workbook is an empty chart sheet.
The existence of this chart sheet in the source workbook allows the workbook to
comply with the requirement of having at least 1 visible sheet after all worksheets
are moved to a new workbook. If this chart sheet doesn't exist, execution of the
Move_All_Worksheets_To_New_Workbook causes a run-time error.
You can use the following statement syntax for purposes of copying a range to the first
(usually single) worksheet of a newly created workbook:
Workbook.Worksheet.SourceRange.Copy
Destination:=Workbooks.Add.Worksheets(1).DestinationRange
1. Copies cells B5 to C20 of the worksheet named “Range”. This cells simply contain some
randomly generated numbers.
2. Pastes the cells it copied in step #1 above in cells B5 to C20 of the first worksheet of a
newly created workbook.
The following image shows the results I get when running this macro. The range is
copied to cells B5 to C20 of the newly created workbook.
The structure I use in the macro example above, however, may not work properly with a
few of the VBA constructs that you can use to copy ranges. A more flexible alternative
involves assigning the newly created workbook to an object variable. After this has
occurred, you can simply refer to the object variable.
The following basic syntax shows how you can (i) create a new workbook, (ii) assign it
to an object variable, and (iii) copy a range to that new workbook.
Workbook.Worksheet.SourceRange.Copy
Destination:=myNewWorkbook.Worksheets(1).DestinationRange
2. Pastes those copied cells in cells B5 to C20 of the first worksheet of a newly created
workbook.
The following screenshot shows the results I obtain when executing the macro. Notice
that they're substantially the same as those obtained with the previous macro example
#15.
Conclusion
Creating a new Excel workbook is one of the most essential and common activities in
Excel. Knowing how to use Visual Basic for Applications for purposes of creating
new workbooks allows you to automate several processes and become an even
more efficient Excel user.
After reading this Excel VBA Tutorial, you have enough knowledge to start or continue
developing a wide array of VBA applications that create new Excel workbooks.
In the first section of this blog post, you read about several VBA constructs that
are commonly used when creating new workbooks. In that section, you focused on the
Workbooks.Add method. This is the most basic and commonly used construct used
within macros that create new workbooks.
In that first section, you also read about other VBA constructs that you can use to
either (i) add functionality and flexibility to the procedures that create new workbooks
or, (ii) create new workbooks without specifically using the Workbooks.Add method.
In the second section of this tutorial, you saw 16 different practical macro
examples that you can easily adjust and start using to create new workbooks. Those
examples of VBA code cover and target a wide variety of situations. The following are
some of the things that these macro examples enable you to do:
Get updates and resources that will help you be more efficient and work faster with
Excel.
Additionally, every Tuesday, I send a short email with an actionable Excel tip to help
you become an Excel Power User.
4. Excel Resources
About | Contact