I am copying the below formula (and several others) and keep getting stuck at ActiveSheet.Name = cell
Excel/Macros Trying to copy sheet2 and rename with list in sheet1 startind withA1- create as many worksheets as their are data in A1.
- DadBurne
- Thread is marked as Resolved.
-
-
Code
Display More'Function extracted from ERLANDSEN DATA CONSULTING Function SheetExists(strSheetName As String) As Boolean ' returns TRUE if the sheet exists in the active workbook SheetExists = False On Error Resume Next SheetExists = Len(Sheets(strSheetName).Name) > 0 On Error GoTo 0 End Function Sub newSheets() Sheets("Sheet1").Activate For Each cell In Sheets("Sheet1").Range("A1", Range("A65536").End(xlUp)) If Not SheetExists(cell.Value) Then Sheets("Sheet2").Copy After:=Sheets(Sheets.Count) ActiveSheet.Name = cell End If Sheets("Sheet1").Activate Next End Sub
-
Hello and Welcome the the Forum
Best thing to do ... is to attach a sample file
-
Does the cell contain a valid sheet name?
I've edited the code a little and added a function to check if the sheet name is usable.
Code
Display MorePrivate Function BadName(s As String) As Boolean BadName = False Dim iBadCharsCount As Integer iBadCharsCount = InStr(1, s, ":") + InStr(1, s, "\") + InStr(1, s, "/") + _ InStr(1, s, "?") + InStr(1, s, "*") + InStr(1, s, "[") + InStr(1, s, "]") If iBadCharsCount > 0 Or Len(s) > 31 Then BadName = True End If End Function Function SheetExists(strSheetName As String) As Boolean ' returns TRUE if the sheet exists in the active workbook SheetExists = False On Error Resume Next SheetExists = Len(Sheets(strSheetName).Name) > 0 On Error GoTo 0 End Function Sub newSheets() With Sheets("Sheet1") For Each cell In Sheets("Sheet1").Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Cells If Not SheetExists(cell.Value) And Not BadName(cell.Value) Then Sheets("Sheet2").Copy After:=Sheets(Sheets.Count) ActiveSheet.Name = cell End If Next End With End Sub
-
For information
- A worksheet name must be unique within a single workbook.
- A worksheet name cannot exceed 31 characters.
- You can use all alphanumeric characters but not the following special characters:
- \ , / , * , ? , : , [ , ].
- You can use spaces, underscores (_) and periods (.) in the name as word separators.
-
-
To make sure the code is in the right place, here's an example workbook
-
The cell has a date 5/1/2021, etc
-
I want the sheet name to be the date (in the list) and I am also filling a cell in the worksheet with that same value (date) through a formula in the worksheet.
-
-
Sorry - I thought I did - new to this forum.
-
-
Looks like that did it - first day on forum - THANKS!!!!
-
After the Macro creates the new sheets, I want it to delete the original sheets - Dates and Templates . file attached.
-
As I said the sheet names you were using were invalid.
As I showed in my code you don't need to Select the sheets.
Code
Display MoreFunction SheetExists(strSheetName As String) As Boolean ' returns TRUE if the sheet exists in the active workbook SheetExists = False On Error Resume Next SheetExists = Len(Sheets(strSheetName).Name) > 0 On Error GoTo 0 End Function Sub newSheets() Dim cell As Range Application.ScreenUpdating = False With Sheets("Dates") For Each cell In .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Cells If Not SheetExists(cell.Value) Then Sheets("Template").Copy After:=Sheets(Sheets.Count) ActiveSheet.Name = Format(cell.Value, "dd-mm-yy") End If Next Application.DisplayAlerts = False .Delete End With Sheets("Dates").Delete Application.DisplayAlerts = True End Sub
-
-
What is the best way/format for me to use the date for the sheet name?
-
-
Whatever suits best avoiding the characters that I mention before.
With the error message, is the sheet named Dates in that workbook. What error is reported?
-
Have you resolved the error?
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!