vba sheet referencing help

bolo

Active Member
Joined
Mar 23, 2002
Messages
423
Hi i was wondering if anyone can clear something up for me.

I have 8 sheets, named cpt1 to cpt8 in vba.

Now i need to loop through these 8 sheets, storing the same range of each sheet into a matrix. I have the following

dim test () as variant

For i = 1 to 8
(1) test = sheets(i).Range("b2:u21")
(2) test = cpt1.Range("b2:u21")
next i

Now (1) doesn't work, but (2) does. However i do not know how to reference the cpt1 sheet name using the for loop (ie "cpt" & i) Can anyone help me with getting this to work?

Thanks

Bolo
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Are these the only sheets?
Code:
For Each ws In Worksheets
     test = ws.Range("b2:u21") 
Next ws
If they aren't we can easily add some condition(s) to the code.
Code:
For Each ws In Worksheets
     If Left(ws.Name,3)="cpt" Then test = ws.Range("b2:u21") 
Next ws
 
Upvote 0
Are these the only sheets?
Code:
For Each ws In Worksheets
     test = ws.Range("b2:u21") 
Next ws
If they aren't we can easily add some condition(s) to the code.
Code:
For Each ws In Worksheets
     If Left(ws.Name,3)="cpt" Then test = ws.Range("b2:u21") 
Next ws

Hi Norrie,

I need to use the i notation really, as the i will be used in calculations later.
 
Upvote 0
Well what VogII posted should work.

And Sheets and Range should work together no problem. :)
 
Upvote 0
Well i know what yogII suggested doesn't work!!!

If you look at the sheets collection property in the help, Range is not mentioned in help. Hence why i thought they don't work together. Plus it is the only thing that is causing the error.
 
Upvote 0
What error?

It's always a good idea to tell us what the error is.:)
 
Upvote 0
Hi,

not sure if it can be done easier
first I would suggest
Code:
Dim test As Variant
without brackets !

this works for me
Code:
Sub testit()
Dim test As Variant
Dim i As Integer

For i = 1 To 8
test = Sheets(ActiveWorkbook.VBProject.VBComponents("cpt" & i).Name).Range("b2:u21")
Next i

End Sub

regards,
Erik
 
Upvote 0
Hi,

not sure if it can be done easier
first I would suggest
Code:
Dim test As Variant
without brackets !

this works for me
Code:
Sub testit()
Dim test As Variant
Dim i As Integer

For i = 1 To 8
test = Sheets(ActiveWorkbook.VBProject.VBComponents("cpt" & i).Name).Range("b2:u21")
Next i

End Sub

regards,
Erik

Erik, do i need to load a reference to make this work?

I found the reference library that i need. However i get a subscript out of range error (run time error 9) with your exact code Erik. Any ideas?
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,845
Members
449,471
Latest member
lachbee

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