Thread: Need to loop
View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
GregR GregR is offline
external usenet poster
 
Posts: 246
Default Need to loop

I have this code, which adds sheets to a workbook based on a named
range. It renames the sheets to the name in the range, but errors on
the second name change because it already exists. How do I get he
macro to rename sheets based on the names in the named list. TIA

Sub AddRangeOfSheets()

Dim c As Range

For Each c In Sheets("sheet1").Range("SList")
If Not IsEmpty(c) Then
CreateSheet (c.Value)
End If
Next c

End Sub

Sub CreateSheet(ShName As String)

Dim CreateSheet As Worksheet

On Error GoTo errh
Set CreateSheet = ActiveWorkbook.Worksheets(ShName)
Exit Sub

errh:
Set CreateSheet = ActiveWorkbook.Worksheets.Add
CreateSheet.Name = ShName

End Sub

Thanks, Greg