I am working on a code to make copies of worksheets along with its name ranges in the same workbook and then promote the scope to workbook.
Following code is used to make copies of worksheet
For A = 1 To 8 For B = 1 To 3 Set ws = Sheets(tabnames(A) & B) ws.Copy After:=Sheets(tabnames(A) & B) ActiveSheet.Name = tabnames(A) & B + 1 Next B Next A Attached Code is used for changing name scope to workbook: https://stackoverflow.com/a/20054925
The code works fine in its first run. Once executed, when I try to rerun, the program returns an error message says:
The Name 'XX' already exists. Click yes to use that version of name, or click No to rename the version of 'XX' you are moving or copying.
The same message pops up when I copy the sheets manually. What confusing me is the same code works well when I reopen the WB. Which means after each run I have to close and open the WB to execute next run. Is there a solution for this? Perhaps something like a refresh or reset?
I tried freezing display alerts, but then I noted that the name ranges are not being copied along with the sheets.
Also, I tried a other codes mentioned in the thread above for promoting name scope and ended up the same.
Also, It works when I copy second last sheet (either manually or via the code below)
Set ws = Sheets(tabnames(A) & B-1) ws.Copy After:=Sheets(tabnames(A) & B) Please Advise.