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.
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.
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 FunctionHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickCreateSheetsFromListand click Run. - Select the cells with the names before you run the macro.
- 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:
wb.Worksheets("Template").Copy After:=wb.Sheets(wb.Sheets.Count)
Set ws = ActiveSheetTo 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.
Related: VBA macro to rename sheets from a list in Excel, VBA macro to create a folder for each name in a list.