I have a workbook with 2 worksheets. The first worksheet is named "COURSEID" with a list of course IDs, the second worksheet is named "Template" that has formulas. I would like to copy the Template worksheet, and name the copies from a list in COURSEID from A2:A77.
The VBA code below is not naming the worksheets, and it's placing the COURSEID list on each of the copied sheets.
Any suggestions?
The VBA code below is not naming the worksheets, and it's placing the COURSEID list on each of the copied sheets.
Any suggestions?
Code:
Sub AddSheets() Dim xRg As Excel.Range
Dim wSh As Excel.Worksheet
Dim sh1 As Excel.Worksheet
Dim wBk As Excel.Workbook
Set wSh = Sheets("COURSEID")
Set sh1 = Sheets("Template")
Set wBk = ActiveWorkbook
Application.ScreenUpdating = False
For Each xRg In wSh.Range("A2:A77")
With wBk
sh1.Copy After:=Sheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = xRg.Value: wSh.Range("A2:A77") = xRg.Value: ActiveSheet.Range("a1") = xRg.Value
If Err.Number = 1004 Then
Debug.Print xRg.Value & " already used as a sheet name"
End If
On Error GoTo 0
End With
Next xRg
Application.ScreenUpdating = True
End Sub