Excel vba to create a new sheet
You can create a new sheet in Excel using VBA by using the Worksheets.Add
method. Here is an example:
Sub CreateNewSheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
ws.Name = "NewSheet"
End Sub
This code creates a new sheet and names it "NewSheet". You can adjust the name and position of the new sheet by modifying the Name
and After
properties.
Here's a breakdown of the code:
Worksheets.Add
creates a new worksheet.After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
specifies the position of the new sheet. In this case, it adds the new sheet after the last existing sheet.Set ws =
assigns the new worksheet to a variable namedws
.ws.Name = "NewSheet"
sets the name of the new sheet.
You can also use the Worksheets.Add
method with other arguments to specify the position and name of the new sheet. For example:
Worksheets.Add(Before:=Worksheets("Sheet1"), Count:=1, Type:=xlWorksheet)
This code adds a new sheet before the "Sheet1" sheet, and names it "NewSheet".
You can also use the Sheets.Add
method to create a new sheet, it's similar to Worksheets.Add
but it creates a new sheet in the active workbook.
Sheets.Add(After:=Sheets(Sheets.Count))
You can also use the Application.Workbooks.Add
method to create a new workbook and then add a new sheet to it.
Dim wb As Workbook
Set wb = Application.Workbooks.Add
wb.Sheets.Add
Note: You need to have the Excel application object (Application
) to use the Application.Workbooks.Add
method.