How to use range object with variable? Example: A1:B3, then A3:B5, then A5:B7

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Shortly: I need the range of A1:B3, then A3:B5 then A5:b7

_______________________________

Parts of my thoughts, but it leads nowhere because I can't make a code from it, these are just parts of it, but I can't combine them in the right way:

So I have 2 variables:

Dim i as integer
dim j as integer

I have two "for" commands which describe the number next to "A" and "B":

for i=1 to 100 step 2
for j=3 to 100 step 2

And the combination:

Range(A&i":"B&j)

And eventually:

random commands

Can you help me how these parts can become a whole? Have a nice day!
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Why not just do:
Code:
Dim i as Integer
Dim rng as Range

For i = 1 to 99 Step 2
    Set rng = Range("A" & i & ":B" & i+2)
    ...
Next i
 
Upvote 0
Try something like this:

Code:
Dim i as integer, MyRange as Range

    For i = 1 to 100 Step 2
        Set MyRange = Range("A1:B3").Offset(i)
    Next i
I'm a little curious why you'd want to have overlapping ranges though.
 
Last edited:
Upvote 0
Thank you!!!!

Yeah, it was a mistake from me :-) I just wanted to know the method because I couldn't find it out. Thanks again :-)
 
Upvote 0
Code:
Dim i as integer, MyRange as Range

    For i = 1 to 100 Step 2
        Set MyRange = Range("A1:B3").Offset(i)
    Next i
Eric,
That won't quite work without a minor adjustment. You either need to start i at 0, or offset by "i-1".
Otherwise, your first range is A2:B4, and you increment by 2 from there.
 
Upvote 0
OK, glad you're happy.

One more thought, I probably should have had

Code:
For i = 0 to 99 Step 2

Because you want to start at A1:B3, and an offset of 0 is the original cell. If you want an offset of rows, and columns, you'd use

Code:
.Offset(r, c)


Edit: Joe4, yup, I belatedly saw that myself. Good eye.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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