Hi,
I'm new to this site and VBA. I came across solution that works fine for me up to the point where I add new items to the list and then Macro tries to overwrite sheets that are already there returning error about the name being taken...
what I have now:
I have workbook with sheets "List" and "Template".
In List I have names/products in column D (starting from D2).
This list will grow in time as I'll be adding new items.
I need code to copy sheet Template and give the new ones the names from list.
Some values are being put in new sheets from List cells as well.
code below does all that.
however, when I add anything to the List (column D) macro tries to do it from scratch and returns error - what I need is modification to the code so that whatever is already in the file is intact and code just adds new sheets for names that are currently added - not sure if it makes sense...(
for example in "List" I already have D2 to D4 taken by inputs X, XX, XXX. I run macro and it created three new sheets named X, XX, and XXX which are copies of Template. I add info to X, XX and XXX sheets and save the file. now I'm opening the file again (all worksheets are there) and add ZZZ into D5 in "List" what I need is to leave everything as is just add new copy of "Template" named ZZZ. change what's needed and save. And so on...
current code:
Sub Create_WS()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("List").Range("d2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
Sheets("Template").copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = MyCell.Value 'this is where code gives error when re-run
Sheets(MyCell.Value).Range("C20").Value = MyCell.Offset(0, -1).Value
Sheets(MyCell.Value).Range("c9").Value = MyCell.Offset(0, 1).Value
Next MyCell
End Sub
I would appreciate your help in getting it sorted.
thanks
I'm new to this site and VBA. I came across solution that works fine for me up to the point where I add new items to the list and then Macro tries to overwrite sheets that are already there returning error about the name being taken...
what I have now:
I have workbook with sheets "List" and "Template".
In List I have names/products in column D (starting from D2).
This list will grow in time as I'll be adding new items.
I need code to copy sheet Template and give the new ones the names from list.
Some values are being put in new sheets from List cells as well.
code below does all that.
however, when I add anything to the List (column D) macro tries to do it from scratch and returns error - what I need is modification to the code so that whatever is already in the file is intact and code just adds new sheets for names that are currently added - not sure if it makes sense...(
for example in "List" I already have D2 to D4 taken by inputs X, XX, XXX. I run macro and it created three new sheets named X, XX, and XXX which are copies of Template. I add info to X, XX and XXX sheets and save the file. now I'm opening the file again (all worksheets are there) and add ZZZ into D5 in "List" what I need is to leave everything as is just add new copy of "Template" named ZZZ. change what's needed and save. And so on...
current code:
Sub Create_WS()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("List").Range("d2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
Sheets("Template").copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = MyCell.Value 'this is where code gives error when re-run
Sheets(MyCell.Value).Range("C20").Value = MyCell.Offset(0, -1).Value
Sheets(MyCell.Value).Range("c9").Value = MyCell.Offset(0, 1).Value
Next MyCell
End Sub
I would appreciate your help in getting it sorted.
thanks