Resize range does not resize address

Lapwing

New Member
Joined
Jan 12, 2015
Messages
15
I have just found something that seems strange to me. If I resize a range the address of that range does not get resized at the same time. This then prevents being able to step through the range using a For Each loop.
Question: do I really have to SET the range so that I can loop through it?

Code:
Sub TestSub()
Dim rTarget As Range
Dim Cell As Range


Set rTarget = Range("A1")
rTarget.Resize(5, 5) = 1


Debug.Print rTarget.Address 'this produces $A$1



Set rTarget = Range("A1:E5")
Debug.Print rTarget.Address '$A$1:$E$5


'if you comment out the previous two lines
'there is only one pass through this bit of code instead of 5


For Each Cell In rTarget.Columns(5).Rows
    Cell = Cell * 5
Next


End Sub
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This doesn't 'permanently' resize rTarget
rTarget.Resize(5, 5) = 1

It only does it temporarily, and puts a 1 in all those cells.
rTarget remains whatever it was previously.

It would be just like doing
rTarget.Offset(5,5) = 1

You don't expect rTarget to then be changed to F6 right?

You'd have to do
Set rTarget = rTarget.Resize(5,5)
 
Upvote 0
Thanks very much Jonmo1 that works as described (both answers).
As you indicated I thought the resize was permanent.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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