Creating a New Worksheet in Excel VBA
If you want to create a new worksheet using VBA in Excel, you can use the following code:
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add
ws.Name = "New Worksheet"
In this example, a new worksheet is added to the workbook using the Sheets.Add
method. The newly created worksheet is then assigned to the variable ws
. You can change the name of the worksheet by modifying the ws.Name
property.
Here’s a complete example that demonstrates the creation of a new worksheet:
Sub CreateNewWorksheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets.Add
ws.Name = "New Worksheet"
' Add data to the new worksheet
ws.Range("A1").Value = "Hello"
ws.Range("B1").Value = "World"
End Sub
In this example, the code creates a new worksheet and sets its name to “New Worksheet”. Then, it adds some text values to cells A1 and B1 of the new worksheet.