Duplicating worksheet error

davez

Board Regular
Joined
Feb 12, 2003
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi all, at my wits end with this problem so hoping someone can help -

Using Excel2007 & I have the following code which I hope to use to create duplicates of a worksheet named 'prec' based on a list of names at cell B21 down in a worksheet named 'CODES' , but keep getting the message 'subscript out of range', with the line Set rngName = ThisWorkbook.Sheets("CODES").range("B21") highlighted.

Thanks for any help provided.


Code:
Sub duplicate_prec_based_on_list()
   Dim rngName As range
   Dim i As Integer
   Set rngName = ThisWorkbook.Sheets("CODES").range("B21")
   Do Until rngName.Value = ""
       i = ThisWorkbook.Sheets.count
       Sheets("prec").Copy After:=Sheets(i)
       ThisWorkbook.Sheets(i + 1).Name = rngName.Value
       Set rngName = rngName.Offset(1)
   Loop
End Sub
 
Well, I rebooted the pc, started from scratch & all is working without issue, guess some gremlins got in the works somewhere, still very puzzling but will put it down to experience.

Thanks for all replies & assistance.
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Yeah, it happens....consider using the code cleaner link that I posted.
It will save you some pain, I guarantee....:beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,800
Messages
6,181,045
Members
453,014
Latest member
Chris258

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top