I would like to create a Macro that will use data from a master worksheet to populate cells in a new worksheet that is based on our template. I found and modified the below code, which, for the most part, seems to do what I want regarding creating the new sheets, but I cannot figure out how to populate the new sheet with data from the corresponding row. The other modification that I would like to make is for the data in an existing sheet to be overwritten in the event that there is already one by the same name.
Option Explicit
Sub SheetsFromTemplate()
'Create copies of a template sheet using text on a master sheet in a specific column
'Sheetname strings are corrected using the UDF below
Dim wsMASTER As Worksheet, wsTEMP As Worksheet, wasVISIBLE As Boolean
Dim shNAMES As Range, Nm As Range, NmSTR As String
Dim cell As Range, rngFY2025 As Range
Dim wb As Workbook
With ThisWorkbook 'keep focus in this workbook
Set wsTEMP = .Sheets("Template") 'sheet to be copied
wasVISIBLE = (wsTEMP.Visible = xlSheetVisible) 'check if it's hidden or not
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetVisible 'make it visible
Set wsMASTER = .Sheets("Master") 'sheet with names
'range to find names to be checked
Set shNAMES = wsMASTER.Range("A3:A" & Rows.Count).SpecialCells(xlFormulas) 'or xlConstants
Set rngFY2025 = wsMASTER.Range("A3", wsMASTER.Cells(Rows.Count, "A").End(xlUp))
Application.ScreenUpdating = False 'speed up macro
For Each Nm In shNAMES 'check one name at a time
NmSTR = FixStringForSheetName(CStr(Nm.Text)) 'use UDF to create a legal sheetname
If Not Evaluate("ISREF('" & NmSTR & "'!A1)") Then 'if sheet does not exist...
wsTEMP.Copy After:=.Sheets(.Sheets.Count) '...create it from template
ActiveSheet.Name = NmSTR '...rename it
NewSheet.Range("A1") = wsMASTER.Range("C" & cell.Row)
NewSheet.Range("B2") = wsMASTER.Range("D" & cell.Row)
NewSheet.Range("B6") = wsMASTER.Range("B" & cell.Row)
NewSheet.Range("B10") = wsMASTER.Range("E" & cell.Row)
NewSheet.Range("B4") = wsMASTER.Range("F" & cell.Row)
NewSheet.Range("B5") = wsMASTER.Range("G" & cell.Row)
NewSheet.Range("B8") = wsMASTER.Range("H" & cell.Row)
NewSheet.Range("B7") = wsMASTER.Range("I" & cell.Row)
End If
Next Nm
wsMASTER.Activate 'return to the master sheet
If Not wasVISIBLE Then wsTEMP.Visible = xlSheetHidden 'hide the template if necessary
Application.ScreenUpdating = True 'update screen one time at the end
End With
MsgBox "All sheets created"
End Sub
Function FixStringForSheetName(shSTR As String) As String
'replace each forbidden character with something acceptable
shSTR = Replace(shSTR, ":", "")
shSTR = Replace(shSTR, "?", "")
shSTR = Replace(shSTR, "*", "")
shSTR = Replace(shSTR, "/", "-")
shSTR = Replace(shSTR, "", "-")
shSTR = Replace(shSTR, "[", "(")
shSTR = Replace(shSTR, "]", ")")
'sheet names can only be 31 characters
FixStringForSheetName = Trim(Left(shSTR, 31))
End Function
james martino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.