Referencing a worksheet using variable name from array

mhessnm

Board Regular
Joined
Apr 12, 2019
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Hello, I am new to the forum and a relatively new beginning VBA programmer in Excel. I have done a lot of looking through various forums and thought I found some code that I could adapt to a project I am working on. The code I am looking for will loop through an array of worksheets and perform a task - there may be a conditional depending on the worksheet.

I found variations of this code in various forums, written by longtime VBA programmers, which seems to have worked for others who have used it. This was copied straight from a forum answer and is very similar to others I've found on websites discussing looping through worksheets. However, I cannot make it work running it as a test, and for the life of me I do not know why it isn't working. The problem comes in the With... statement - when the worksheet is referenced by the variable name, it crashes with a run-time error 9 subscript out of range.

Here is the code:

Sub test_loop()


Dim WshtNames As Variant
Dim WshtNameCrnt As Variant


WshtNames = Array("Prison", "PIIP", "Detox")


For Each WshtNameCrnt In WshtNames


With Worksheets(WshtNameCrnt) 'Runtime error 9 subscript out of range here


Debug.Print "Cell B1 of worksheet " & .Name & " contains " & .Range("B1").Value

End With


Next WshtNameCrnt

End Sub

If possible, I'd like to understand why the program crashes there - I have referenced worksheets with variable names in the past with no problem though the variables were declared as strings. I suspect this has something to do with declaring the variable as a variant (and I admit I don't understand variable declarations as well as I should) and that it is not being passed in string form, but my understanding is that For Each needs the variable declared as variant or object.

I would appreciate any advice or help, and thank you in advance from this beginner.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Intead of this:
Code:
WshtNames = Array("Prison", "PIIP", "Detox")
For Each WshtNameCrnt In WshtNames
With Worksheets(WshtNameCrnt) 'Runtime error 9 subscript out of range here
Debug.Print "Cell B1 of worksheet " & .Name & " contains " & .Range("B1").Value
End With
Next WshtNameCrnt
Try this:
Code:
Dim i As Long
WshtNames = Array("Prison", "PIIP", "Detox")
For i = LBoundWshtNames) To UBound(WshtNames)
   Debug Print Worksheets(WshtNames(i)).Range("B1").Value 
End With
Next
 
Upvote 0
You're code works fine for me.
Check that the names in the array are spelt correctly including any leading/trailing spaces.
 
Upvote 0
Solution
Okay, now I feel stupid. Thank you, Fluff. I checked and one of the worksheet names had a trailing space that I wasn't aware of. I'm sorry I wasted time with this - but I was literally racking my brain trying to understand why this didn't work and was undone by a simple typing error. Thank you for your patience!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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