Populate an array without Iteration

Joseph.Marro

Board Regular
Joined
Nov 24, 2008
Messages
153
Hello,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I have searched everywhere and I am not sure if I can even accomplish this task. I know you can populate range values into an array without iteration. However, is it possible to convert a range into an array of row numbers without iteration? <o:p></o:p>
<o:p></o:p>
Example:
Code:
Sub Example()
Dim myRange As Range
Dim myArray() As Variant
Set myRange = Range("A23:A31")
myArray = myRange
Set myRange = Nothing
End Sub
<o:p></o:p>
How do I Modify this to produce an array containing the row numbers {23,24,25,26,27,28,29,30,31}? Is it even possible without iteration?<o:p></o:p>
<o:p></o:p>
Thank you,<o:p></o:p>
<o:p></o:p>
Joseph Marro<o:p></o:p>
<o:p></o:p>
 

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
You could try this.
Code:
Dim rng As Range
Dim x
Set rng = Range("A12:A21")

x = Evaluate("ROW(" & rng.Address & ")")
 
x = Application.Transpose(x)
Though I have to ask why you want/need an array with the row numbers of a range?
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG20May18
[COLOR="Navy"]Dim[/COLOR] Ray
MsgBox Join(Application.Transpose(Evaluate("row(" & Range("A23:A31").Address & ")")))
'[COLOR="Green"][B]Hold data in Array[/B][/COLOR]
Ray = Application.Transpose(Evaluate("row(" & Range("A23:A31").Address & ")"))
MsgBox Join(Ray)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Norie,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Thank you for your solution, it accomplishes the task. I had not thought to use Evaluate and Row() to return the array. What I am trying to accomplish is a custom match function using some fuzzy logic. I know I could use a For Each Cell In Range loop to accomplish the task, but it is horrifically slow. I was thinking it would be faster to stuff the entire range into an array and loop through it using a For Next Loop. The issue I was having is how to preserve the row numbers to be returned later. Am I attacking this problem from the wrong angle? Any suggestions would be greatly welcomed<o:p></o:p>
<o:p></o:p>
Thank you,<o:p></o:p>
<o:p></o:p>
Joseph Marro<o:p></o:p>
<o:p></o:p>
 
Last edited:
Upvote 0
Joseph

Are you sure it's the For Each that's slowing things down?

If you do just want the row numbers for a loop couldn't you use something like this?
Code:
For intRow = Range("A21").Row To Range("A34").Row
Note, that would only work if the range is contiguous, which I think it might not be.:)
 
Upvote 0
Joseph

Are you sure it's the For Each that's slowing things down?

If you do just want the row numbers for a loop couldn't you use something like this?
Code:
For intRow = Range("A21").Row To Range("A34").Row
Note, that would only work if the range is contiguous, which I think it might not be.:)

I remember reading somewhere that a For Each loop is actually faster than a For [var] x to y loop when dealing with a 1-dimensional array. I'll try to dig up that info again.

Edit: It was on Chip Pearson's site - http://www.cpearson.com/excel/optimize.htm
 
Last edited:
Upvote 0
I can do it so fast I don't even need an array at all:
Code:
For i = 23 to 34: Next

;)
 
Upvote 0
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>
Maybe I am incorrect but I was under the impression that a for loop on an array is significantly faster than a for each loop on a range. I believe this is true for most collections. The only issue I see is how to efficiently populate the array.<o:p></o:p>
<o:p></o:p>
Thank you,<o:p></o:p>
<o:p></o:p>
Joseph Marro<o:p></o:p>
<o:p> </o:p>
 
Upvote 0
Joseph

Didn't you say your For Each was 'horrificly slow'?

Perhaps it is the code within the loop which is slow.

We've not seen that but if you are using ranges there it could be.

Really depends what it's doing I suppose.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,683
Members
452,938
Latest member
babeneker

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