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
 
Yes they are the code name for the sheets (as seen in the properties window fist line). All the sheets are in the active workbook, I have my code in module1

Using just nameArr(0).Select gives me error 424

If I push the name as shown in the worktabs into the array it does work with the Sheets(nameArr(i)) line you gave me but I was hoping to use the code name just in case someone renamed my worksheets
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Is the code in the same workbook as the sheets you are trying to access?
 
Upvote 0
In that case something lie this should work
VBA Code:
   Dim nameArr As Variant
   Dim i As Long
   
   nameArr = Array(Pending, Dossier, Sheet3, Sheet5, Sheet4, Sheet2)
   For i = 0 To UBound(nameArr)
      Debug.Print nameArr(i).Name
   Next i
If you get an error 424 Object required, then that particular sheet does not exist in the workbook with the code.
 
Upvote 0
Brilliant thanks Fluff, I knew it was something simple I can't believe it was just the quotation marks around my array of names.

So appreciative of your help!
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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