Using range array with worksheet array

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
556
Office Version
  1. 2016
Platform
  1. Windows
Hi,
Hoping someone can sort this one for me.
I have an array of worksheets called MySht and an array of ranges called MyRng. What I am trying to do is loop through the range array with the first worksheet and then move to the second worksheet and do the same. I was hoping to do it something like :

VBA Code:
For each ws in MySht
   For each Rng in MyRng
      set srchrng = ws.rng
         'CODE THEN DOES SOME OTHER BITS
   next rng
next ws

code is failing on the ws.rng bit

what am i doing wrong??
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
A range value is already a set range, with worksheet reference built into it (if you do not qualify the range with a worksheet reference, it defaults to the active sheet).
So it doesn't make sense to prefix a range variable with a worksheet variable (since the range variable is already attached to a sheet, by definition).

Here is a simple example to show that:
VBA Code:
Sub Test()

    Dim ws As Worksheet
    Dim rng As Range
   
    Set ws = Sheets("Sheet2")
    Set rng = Range("A1")
   
    MsgBox rng.Value
    'MsgBox ws.rng.Value
   
End Sub
If you run this code, it will return the value from cell A1 of the ActiveSheet.
However, if you uncomment the second MsgBox (and try to put a worksheet reference in front of the range), you will see it is not a valid reference and you will get an error.

To do what you want, it would be better to store the range addresses as strings in your array, and then refer to them like ws.Range(rng), as shown in this simple example:
VBA Code:
Sub Test2()

    Dim ws As Worksheet
    Dim rng As String
   
    Set ws = Sheets("Sheet2")
    rng = "A1"
   
    MsgBox ws.Range(rng).Value
   
End Sub
 
Upvote 0
Solution
A range value is already a set range, with worksheet reference built into it (if you do not qualify the range with a worksheet reference, it defaults to the active sheet).
So it doesn't make sense to prefix a range variable with a worksheet variable (since the range variable is already attached to a sheet, by definition).

Here is a simple example to show that:
VBA Code:
Sub Test()

    Dim ws As Worksheet
    Dim rng As Range
  
    Set ws = Sheets("Sheet2")
    Set rng = Range("A1")
  
    MsgBox rng.Value
    'MsgBox ws.rng.Value
  
End Sub
If you run this code, it will return the value from cell A1 of the ActiveSheet.
However, if you uncomment the second MsgBox (and try to put a worksheet reference in front of the range), you will see it is not a valid reference and you will get an error.

To do what you want, it would be better to store the range addresses as strings in your array, and then refer to them like ws.Range(rng), as shown in this simple example:
VBA Code:
Sub Test2()

    Dim ws As Worksheet
    Dim rng As String
  
    Set ws = Sheets("Sheet2")
    rng = "A1"
  
    MsgBox ws.Range(rng).Value
  
End Sub
Joe4 - string option worked perfectly. thank you
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

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