Create new worksheet in excel vba
You can create a new worksheet in Excel VBA using the following code:
Sub CreateNewWorksheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets.Add
ws.Name = "New Worksheet"
End Sub
This code will create a new worksheet and name it "New Worksheet". You can change the name to whatever you want.
Here's how you can use this code:
- Open the Visual Basic Editor in Excel by pressing
Alt + F11
or by navigating toDeveloper
tab and clicking onVisual Basic
. - In the Visual Basic Editor, click on
Insert
>Module
to insert a new module. - Paste the code into the module.
- Click
Run
>Run Sub/User Form
or pressF5
to run the code. - A new worksheet will be created and named "New Worksheet".
Note: The ThisWorkbook.Worksheets.Add
method adds a new worksheet at the end of the workbook. If you want to add a new worksheet at a specific position, you can use the Insert
method instead:
Set ws = ThisWorkbook.Worksheets.Insert(after:=ThisWorkbook.Worksheets("Existing Worksheet"))
Replace "Existing Worksheet" with the name of the worksheet where you want to add the new worksheet.