Array of worksheet names

ldarley

Board Regular
Joined
Apr 10, 2012
Messages
106
Office Version
  1. 2019
  2. 2016
Hi

Not sure what is going on here obviously I am doing something wrong!

I have an array of worksheet names, it is very simple:

VBA Code:
Dim nameArr As Variant
        nameArr = Array("Pending", "Dossier", "Sheet3", "Sheet5", "Sheet4", "Sheet2")

I was hoping to cycle through the sheets to insert a picture using a For Next Loop however I am getting a runtime error 424 when I try to use nameArr(i) to identify the sheet to add the picture to (i has been declared as an integer).

Same error if I just do a simple nameArr(0).Select

How should I be taking the worksheet name from the array and dropping it back into the code?

Many Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You need to refer to it like
VBA Code:
Sheets(nameArr(i)).Select
 
Upvote 0
Thanks Fluff I have modified this but now I get a runtime error 9 subscript out of range whichever nameArr () value I use?
 
Upvote 0
In that case make sure that the names in the array are exactyy the same as shown on the sheet tab, especially check for any leading/trailing spaces.
 
Upvote 0
Weird as it doesn't seem to be that e.g.

If I put in Pending.Select the code selects the Pending worksheet but if I put in

Sheets("Pending").Select I get the runtime error 9
 
Upvote 0
In that case you are using the codename, not the sheet name, so you need to remove the quotes from inside the array & then you can use nameArr(0).Select
 
Upvote 0
Weird as it doesn't seem to be that e.g.

If I put in Pending.Select the code selects the Pending worksheet but if I put in

Sheets("Pending").Select I get the runtime error 9

That will work if you change your sheet property (name) to Pending in VBA and if so then the code below will also work without the parenthesis...
VBA Code:
Sheets(Pending).Select
 
Upvote 0
That's right if I take out the parenthesis I get a type mismatch error 13
 
Upvote 0
Are those values the actual sheet name as seen on the tab, or are they the sheet codename which is the part before the brackets when you look at the project window in the editor?
1602766086044.png

Also are those sheets in the same workbook as the code?
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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