can anyone help me with this, pls?
I’m looking for a solution which would follow those steps below:
1 – Every time a new entry (new row) under column A (let’s say “Client Name”) in the tab called Summary, then copy template worksheet (called Template) and link cell and name sheet against new entry name
2 – VBA automation would check if any tab created previously has the same name, if it has then it should pops up a message “Template with same “Client ABC” was created previously.
I have tried this VBA below, but this one doesn’t work when column A (“Client Name”) has new entries… this VBA only works once. Like having a blank table, then filling up with customer names, then running it once to copy the template against each customer name and linking it.
Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet
If wb Is Nothing Then Set wb = ThisWorkbook
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
SheetExists = Not sht Is Nothing
End Function
Sub CreateSummarySheets(SummaryWS As Worksheet, TemplateWS As Worksheet)
Dim newWS As Worksheet
Dim rCell As Range
Dim lastRow As Long
Dim answer As Long
lastRow = SummaryWS.Cells(Rows.Count, "A").End(xlUp).Row
For Each rCell In SummaryWS.Range("$A$2:$A$" & lastRow)
'Add copy of template
TemplateWS.Copy After:=Sheets(Sheets.Count)
Set newWS = Sheets(Sheets.Count)
'Sheet exists error checking
answer = 1
If SheetExists(newWS.Name) = False Then
answer = vbNo
answer = MsgBox("Sheet with the name " & rCell.Value & " already exists. Delete it?", vbYesNo, rCell.Value & " Sheet Exists")
End If
If answer = vbYes Then
Sheets(rCell.Value).Delete
End If
If answer = 1 Or answer = vbYes Then
newWS.Name = rCell.Value
End If
'Populate newWS's cell A1
newWS.Cells(1, "A") = rCell.Value
'Add Hyperlink from summary to newWS
newWS.Hyperlinks.Add Anchor:=rCell, Address:="", _
SubAddress:="'" & newWS.Name & "'" & "!A2", TextToDisplay:=newWS.Name
Next rCell
End Sub
Sub test()
Dim s_ws As Worksheet
Set s_ws = Sheets("Summary")
'Two ways to run this function
Call CreateSummarySheets(s_ws, Sheets("Template"))
End Sub
Guilherme Provenzano Zimmerman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.