VBA Code Help

This is the first of a series of blogs containing useful pieces of code.

Adding A New Worksheet into A Workbook

The following piece of code allows you to add a new Worksheet into your Workbook.

Sub NewWorksheet()

    Sheets.Add
    
End Sub

We can develop this code to allow us to choose a name for our new Worksheet.

Sub NewWorksheetAndName()
    Dim strName As String
    
    strName = InputBox("What would you like to name your sheet?")

    Sheets.Add
    ActiveSheet.Name = strName
    
End Sub

We can develop this further to allow us to add the new Worksheet after the last Worksheet in the Workbook.

Sub NewWorksheetAfterLastSheet()
    Dim strName As String
    
    strName = InputBox("What would you like to name your sheet?")

    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Name = strName
    
End Sub

For more help with Excel VBA, keep an eye on our blog, or come and join us on one of our courses, Introduction to Excel VBA & Advanced Excel VBA.

Share this post