novicecoder
New Member
I've created a macro which is supposed to take inputs from a certain sheet, as in "Docs" from a workbook and create as many new worksheets as many inputs are there. In my case, I've been trying to create three new sheets with this three inputs "AB","BC" and "CD" which are in Range("A1") to Range("A3") in column A. However, when I run my script, It only creates a new sheet with the name "AB" whereas I was expecting to have three new sheets with the three different names. The important thing is the script will first look for the three sheets whether they already exist. If they are not then the macro will create them. Where I'm going wrong with my following attempt and how can I fix it?
This is the macro I've written:
This is the macro I've written:
Code:
Sub NameNewSheet()
Dim ws As Worksheet, shtname As Range
For Each shtname In Sheets("Docs").Range("A1:A" & Sheets("Docs").Range("A" & Rows.Count).End(xlUp).row)
With ThisWorkbook
On Error Resume Next
Set ws = .Sheets(shtname)
On Error GoTo 0
If ws Is Nothing Then
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = shtname
End If
End With
Next shtname
End Sub