VBA Routine to Add and Name Worksheets

October 11th, 2008 | Categories: Worksheets | Tags: , , , ,

The following routine will look at the contents of a single column set up Excel worksheets within the current workbook with these names. It makes a call to another function to see if a sheet with that name already exists, and if so the sheet isn’t created.

Private Sub CommandButton1_Click()

Call CreateWorksheets(Sheets("Sheet2").Range("A1:a10"))

End Sub

Sub CreateWorksheets(Names_Of_Sheets As Range)
Dim No_Of_Sheets_to_be_Added As Integer
Dim Sheet_Name As String
Dim i As Integer

No_Of_Sheets_to_be_Added = Names_Of_Sheets.Rows.Count

For i = 1 To No_Of_Sheets_to_be_Added

Sheet_Name = Names_Of_Sheets.Cells(i, 1).Value

'Only add sheet if it doesn't exist already and the name is longer than zero characters

If (Sheet_Exists(Sheet_Name) = False) And (Sheet_Name <> "") Then
    Worksheets.Add().Name = Sheet_Name
End If

Next i

End Sub
Function Sheet_Exists(WorkSheet_Name As String) As Boolean
Dim Work_sheet As Worksheet

Sheet_Exists = False

For Each Work_sheet In ThisWorkbook.Worksheets

    If Work_sheet.Name = WorkSheet_Name Then
        Sheet_Exists = True
    End If

Next

End Function

So if we have the following text in cells A1:A30 in Sheet 2:

Then the following sheets will be created:

Note that although “Dog” appears twice, only one sheet is created.

To download the .XLS file for this tutorial, click here.

  1. Excel Dude
    December 15th, 2009 at 20:40
    Reply | Quote | #1

    What if I do not want to create sheets for the cells contains no data? And also, instead of setting up blank sheets, I would like to copy a templete sheet (named Temp for example) for the each cell with data on Column A in Sheet1. Can you please help me on this? I am a beginner in VBA and don’t know what exacylt to do. Thanks.