How to return a limited section of an array?

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
I have the following formula which sums the first i rows and j columns of a range ("MyRange") using Resize:

WorksheetFunction.Sum(MyRange.Resize(i, j))

Now is there an equivalent VBA function which can be used with a general VBA array (and not just a range)?
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
If you are asking about 'resizing' an array, read the help topic on ReDim and Preserve.
 
Upvote 0
Thanks, but ReDim physically changes the size of an array, whereas I only want to select, return or "read" a section of an array without changing the original.
 
Upvote 0
Ahh, my bad. You can loop using a For n = [some number] To [some other number], where you can set the low number to higher than the array's base and/or set the upper limit to below the array's upper bound. Does that make sense?
 
Upvote 0
Yes, it does make sense, but looping is what I was trying to avoid (too slow). There must be a VBA function that can select only a sub-section of an array, as you can with a range, no?

For example: MyArray(1 to i, 1 to j)

Isn't there something similar that works?
 
Last edited:
Upvote 0
Not AFAIK. You can you a worksheet function (INDEX) to return a column or row of an array.

I would mention that in your thinking that .Resize is returning a 'sub-section', I would think of it returning a new/additional range object.

BTW, whilst looping thru cells in a range is slow, looping one actual array into another seems pretty quick to me.
 
Last edited:
Upvote 0
OK, thanks. It's a pity that, while Offset can also be used to reset height and width (number of rows and columns) as well as absolute offset as a worksheet function, it can only be used to set an absolute offset in VBA.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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