C create new workbook
Here is an example of how to create a new workbook in Excel using VBA:
Visual Basic Editor
- Open the Visual Basic Editor by pressing
Alt + F11
or by navigating toDeveloper
>Visual Basic
in the ribbon. - In the Visual Basic Editor, click
Insert
>Module
to insert a new module. - In the module, add the following code:
Sub CreateNewWorkbook() Dim wb As Workbook Set wb = Workbooks.Add wb.SaveAs "C:\Path\To\NewWorkbook.xlsx" End Sub
Explanation
Workbooks.Add
creates a new workbook.Set wb = Workbooks.Add
assigns the new workbook to a variable namedwb
.wb.SaveAs "C:\Path\To\NewWorkbook.xlsx"
saves the workbook to the specified file path and name.
Run the code
- Click
Run
>Run Sub/User Form
or pressF5
to run the code. - The new workbook will be created and saved to the specified file path.
Note
- Make sure to replace
C:\Path\To\NewWorkbook.xlsx
with the actual file path and name you want to use. - If you want to create a new workbook without saving it, simply remove the
wb.SaveAs
line.