VBA macros · Sheets

VBA macro to create sheets from a list of names in Excel

Select a list of names and create one new worksheet for each, with invalid and duplicate names reported instead of crashing the macro.

Windows + Mac

One sheet per month, per employee, per project: building the skeleton of a workbook means adding and naming many sheets. Type the names in a column, select them and run this macro. It adds a sheet for every name at the end of the workbook, skips names that already exist and tells you about names Excel does not accept.

VBA
Option Explicit

Sub CreateSheetsFromList()
    Dim lst As Range, c As Range
    Dim wb As Workbook
    Dim startSheet As Object
    Dim ws As Worksheet
    Dim nm As String
    Dim made As Long
    Dim skipped As String

    If TypeName(Selection) <> "Range" Then Exit Sub
    Set lst = Selection
    Set wb = ActiveWorkbook
    Set startSheet = ActiveSheet

    If wb.ProtectStructure Then
        MsgBox "Unprotect the workbook structure first.", vbExclamation
        Exit Sub
    End If

    Application.ScreenUpdating = False

    For Each c In lst.Cells
        nm = Trim$(c.Text)
        If Len(nm) > 0 Then
            If SheetExists(wb, nm) Then
                skipped = skipped & vbLf & nm & " (already exists)"
            Else
                Set ws = wb.Worksheets.Add(After:=wb.Sheets(wb.Sheets.Count))
                On Error Resume Next
                ws.Name = nm
                If Err.Number <> 0 Then
                    ' Name not allowed: remove the blank sheet again
                    Application.DisplayAlerts = False
                    ws.Delete
                    Application.DisplayAlerts = True
                    skipped = skipped & vbLf & nm & " (invalid name)"
                Else
                    made = made + 1
                End If
                On Error GoTo 0
            End If
        End If
    Next c

    startSheet.Activate
    Application.ScreenUpdating = True

    If Len(skipped) > 0 Then
        MsgBox made & " sheet(s) created. Skipped:" & skipped, vbExclamation
    Else
        MsgBox made & " sheet(s) created.", vbInformation
    End If
End Sub

Private Function SheetExists(wb As Workbook, ByVal nm As String) As Boolean
    Dim sh As Object
    On Error Resume Next
    Set sh = wb.Sheets(nm)
    SheetExists = Not sh Is Nothing
End Function
Create one sheet for each name in the selection

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick CreateSheetsFromList and click Run.
  5. Select the cells with the names before you run the macro.
  6. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

The selection is stored in lst right at the start. That matters: adding a sheet makes the new sheet active, which changes what Selection means, so the macro must not keep asking for Selection inside the loop. For the same reason it remembers the starting sheet and activates it again at the end.

SheetExists is a tiny helper function. It tries to grab the sheet by name with errors switched off; if that fails, sh stays Nothing and the function returns False. You will see the same pattern in many macros on this site, and in Functions and Subs: parameters, ByVal, ByRef and return values.

New sheets are added After:=wb.Sheets(wb.Sheets.Count), which is always the last position, so they appear in the same order as your list. If Excel refuses the name (too long, contains : \ / ? * [ ], or is the reserved word History), the blank sheet that was just added is deleted again with alerts turned off, so you are not left with stray Sheet7 tabs.

Change it

To create copies of a template sheet instead of blank sheets, replace the Worksheets.Add line with a copy. The copy becomes the active sheet, so pick it up with ActiveSheet:

VBA
                wb.Worksheets("Template").Copy After:=wb.Sheets(wb.Sheets.Count)
                Set ws = ActiveSheet
Copy a sheet called Template for each name

To write the name into the new sheet as a heading, add ws.Range("A1").Value = nm after the successful rename. To create the sheets in a different workbook, set wb to it, for example Set wb = Workbooks("Plan.xlsx"). Afterwards you can build an index with VBA macro to create a table of contents with hyperlinks in Excel.

What can go wrong

Sheet names may be at most 31 characters long, so long names from a database are rejected. Shorten them in the list first, for example with =LEFT(A2,31). Duplicates inside your own list are caught too: the second one is skipped as already exists.

Watch out. Copying a template sheet that contains defined names can trigger a dialog about names that already exist for every copy. Answer it, or remove the sheet-level names from the template first.
Tip. Works on Windows and Mac. A workbook can hold as many sheets as memory allows, but beyond a few hundred Excel becomes slow to open and save.

Related: VBA macro to rename sheets from a list in Excel, VBA macro to create a folder for each name in a list.

More vba macros