Forum

add new worksheets ...
 
Notifications
Clear all

add new worksheets with names from a range in a sheet

3 Posts
2 Users
0 Reactions
61 Views
(@abdelsurf)
Posts: 2
New Member
Topic starter
 

Please, I need your help.
in range A1:A50 in sheet "name" i have the names of new sheets i would like to add.
my code below to add those sheets is working well, however, if i start the code again he also add worksheets named: name1, name2, name3 etc..
please help to solve this.
With many thanks.

Sub test()

On Error Resume Next
Sheets("naam").Select
Dim template As Range
For Each template In Range("A1:A5")

If template.Offset(0, 0).Value <> "" Then
Dim tabblad As Worksheet
Set tabblad = ThisWorkbook.Worksheets("naam")
tabblad.Copy ThisWorkbook.Sheets(Sheets.Count)

With ThisWorkbook

ActiveSheet.Name = template.Offset(0, 0).Value

End With

Else
Exit Sub
End If
Next

end sub

 
Posted : 04/12/2018 7:26 am
(@debaser)
Posts: 837
Member Moderator
 

You could check to see if the sheets already exist like this:

 

Sub test()

Dim template As Range
Dim tabblad As Worksheet
Set tabblad = ThisWorkbook.Worksheets("naam")

For Each template In Sheets("naam").Range("A1:A5")

If template.Value <> "" Then
If Not Application.Evaluate("ISREF('" & template.Value & "'!A1)") Then
tabblad.Copy ThisWorkbook.Sheets(Sheets.Count)
ActiveSheet.Name = template.Value
End If
Else
Exit Sub
End If
Next

End Sub

 
Posted : 04/12/2018 10:06 am
(@abdelsurf)
Posts: 2
New Member
Topic starter
 

Yes it works!. many thanks Velouria. Great!.

 
Posted : 05/12/2018 6:41 am
Share: