Macro Speed Question

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Macro Speed Question

Post by Goshute »

I'm thinking about an analytic macro that runs a number of internal loops. Generally I set typed ranges in these kind of macros, but some of them are running against up to 100,000 lines of data and the one I'm contemplating has a couple of internal loops and I expect will take a little while. Would there be any advantage in reading the data into a two dimensional array and running the analysis against the array?
Goshute
I float in liquid gardens

User avatar
HansV
Administrator
Posts: 78592
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro Speed Question

Post by HansV »

I performed a quick test with a 100000 rows by 2 columns range. Using a two-dimensional array, the macro was almost 10 times faster than using a Range object.
Best wishes,
Hans

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Macro Speed Question

Post by Goshute »

HansV wrote:Using a two-dimensional array, the macro was almost 10 times faster than using a Range object.
That's pretty darned conclusive! Thanks, Hans.
Goshute
I float in liquid gardens

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Macro Speed Question

Post by Goshute »

HansV wrote:Using a two-dimensional array...
An observation and two questions:

Sub Range2Array()
Dim varRange As Variant

varRange = Sheets("Sheet1").Range("A1:F100")
Debug.Print varRange(5, 4) ' returns content of cell D5
End Sub

This is a 1-based array - I was expecting a zero based array. Am I missing something?

In playing with this I found that it works the same if I Set the array as an object

Set varRange = Sheets("Sheet1").Range("A1:F100")

If I don't use Set is still it good practice to set Variants to nothing when they are equivalent to objects, or does VBA do that for me?
Goshute
I float in liquid gardens

User avatar
HansV
Administrator
Posts: 78592
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro Speed Question

Post by HansV »

1. Rows and columns start counting at 1, so the array is one-based.
2. If you use

varRange = Sheets("Sheet1").Range("A1:F100")

varRange will be of type variant array, but if you use

Set varRange = Sheets("Sheet1").Range("A1:F100")

varRange will be of type Range.

3. All local variables will be set to Nothing automatically when the macro ends, so it's only really necessary to set them to Nothing explicitly if you want to release memory during the course of a macro.
Best wishes,
Hans

User avatar
Goshute
3StarLounger
Posts: 397
Joined: 24 Jan 2010, 19:43
Location: Salt Lake City, Utah, USA

Re: Macro Speed Question

Post by Goshute »

(Next time remind me to use TypeName() :stupidme:)
HansV wrote:2. If you use

Set varRange = Sheets("Sheet1").Range("A1:F100")

varRange will be of type Range.
In which case I'd lose the speed advantage, right?
Last edited by Goshute on 14 May 2010, 22:26, edited 1 time in total.
Goshute
I float in liquid gardens

User avatar
HansV
Administrator
Posts: 78592
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro Speed Question

Post by HansV »

Yes, I fear so.
Best wishes,
Hans