Home ➜ VBA Tutorial ➜
Once you start learning VBA one of the coolest things you can do is to write a VBA code to insert new a worksheet in a workbook.
Well, there is already a shortcut key to insert a new worksheet or you can also use the normal option but the benefit of using a VBA code is you can add multiple worksheets with a single click and you can also define that where you want to add it.
VBA Beginners will find the VBA Time Saver Kit especially useful when trying to reference available VBA Functions. The Kit groups most available VBA Functions into 4 categories: Dates, Strings, Files and Conversions – all linking out to function definitions here at AnalystCave. It contains ready easy to reference examples of VBA. Excel shortcut keys allow you to perform certain tasks using only the keyboard. The idea being that you increase your efficiency when you limit the number of instances your hands have to move back and forth from the keyboard to the mouse. Getting in the habit of using these shortcut keys can help work more.
For this, you need to use the Sheets.Add method, and in this post, we will be learning how to use it to add one or more worksheets in a workbook.
- Before: To add a new sheet before a sheet.
- After: To add the new sheet before a sheet.
- Count: Number of sheets to add.
- Type: Type of the sheet you want to add (LINK)
Write a VBA Code to ADD a New Sheet in a Workbook
Open the visual basic editor and follow these steps.
- First, you need to enter Sheets.Add method.
- Then you need to define the place to add the new sheet (Before or After).
- Next thing is to enter the count of worksheets.
- In the end, the type of sheet.
Excel Vba Functions Cheat Sheet
Different Ways to Add New Sheets in a Workbook using a VBA Code
Below you have different ways to add a new sheet in a workbook:
Vba Syntax Cheat Sheet Excel
1. Add a Single Sheet
To add a single sheet, you can use the below code, where you didn’t specify any argument.
This code tells Excel to add a sheet in the active workbook, but as you don’t have any argument it will use the default values and add one worksheet(xlWorksheet) before the active sheet.
Here’s one more way to write this, check out the below code.
As you are already in the active workbook you can use the below code as well. It does the same thing.
2. Add Multiple Sheets
To add multiple sheets in one go, you just need to define the COUNT argument with the number of sheets you want to add.
Now the count of the sheets that you have defined is 5, so when you run this code it instantly adds the five new sheets in the workbook.
3. Add a Sheet with a Name
If you want to rename the sheet after adding it, you can use the following code:
In the above code, we have used the name object (LINK) which helps you to specify the name of a sheet.
4. Add a Sheet with a Name from a Cell
You can also take the value to use as the sheet’s name from a cell.
In the above code, cell A1 is used to get the name for the new sheet.
5. Add a Sheet After/Before a Specific Sheet
As these arguments are already there in the Sheets.Add where you can specify the sheet to add a new sheet before or after it.
Vba Syntax Cheat Sheet Pdf
Now in the above code, you have two lines of code where you have used before and after an argument in the Sheet.Add method. So, when you run this code it adds two sheets one is before and one is after the “mySheet”.
6. Add a New Sheet at Beginning
By using the before argument using you can also add a sheet at the beginning of the sheets that you have in the workbook.
So basically, what we are going to do is we’re going to specify the sheet number instead of the sheet name.
In the above code, you have used the sheet number (1) that tells VBA to add the sheet before the sheet which is on the first position in all the worksheets. In this way, it will always add the new sheet at the beginning.
7. Add a New Sheet at End (After the Last Sheet)
To add a new sheet in the end you need to write the code in a different way. So, for this, you need to know how many sheets there in the workbook are so that you can add a new sheet at the end.
In the above code, Sheet.Count returns the count of the sheets that you have in the workbook, and as you have defined the after argument it adds the new sheet after the last sheet in the workbook.
8. Add Multiple Sheets and use Names from a Range
The following code counts rows from the range A1:A7. After that, it loops to add sheets according to the count from the range and use values from the range name the sheet while adding it.
But with the above code, there could be a chance that the sheet name you want to add already exists or you have a blank cell in the name range.
In that case, you need to write a code that can verify if the sheet with the same name already exists or not and the cell from where you want to take the sheet name is blank or not.
If both conditions are fulfilled only then it should add a new sheet. Let me put it in steps two steps:
First, you need to write an Excel User Defined Function to check if a sheet with the same name already exists or not.
Second, you need to write a code using this function and that code should also check if the name cell is blank or not.
Now in the above code, you have used the VBA IF Statement and in this statement, you have the sheet check function which checks for the sheet name and then you have a condition to check if the name cell has a blank value.