Macro Speed Question
-
- 3StarLounger
- Posts: 397
- Joined: 24 Jan 2010, 19:43
- Location: Salt Lake City, Utah, USA
Macro Speed Question
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
I float in liquid gardens
-
- Administrator
- Posts: 78592
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro Speed Question
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
Hans
-
- 3StarLounger
- Posts: 397
- Joined: 24 Jan 2010, 19:43
- Location: Salt Lake City, Utah, USA
Re: Macro Speed Question
That's pretty darned conclusive! Thanks, Hans.HansV wrote:Using a two-dimensional array, the macro was almost 10 times faster than using a Range object.
Goshute
I float in liquid gardens
I float in liquid gardens
-
- 3StarLounger
- Posts: 397
- Joined: 24 Jan 2010, 19:43
- Location: Salt Lake City, Utah, USA
Re: Macro Speed Question
An observation and two questions:HansV wrote:Using a two-dimensional array...
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
I float in liquid gardens
-
- Administrator
- Posts: 78592
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Macro Speed Question
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.
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
Hans
-
- 3StarLounger
- Posts: 397
- Joined: 24 Jan 2010, 19:43
- Location: Salt Lake City, Utah, USA
Re: Macro Speed Question
(Next time remind me to use TypeName() )
In which case I'd lose the speed advantage, right?HansV wrote:2. If you use
Set varRange = Sheets("Sheet1").Range("A1:F100")
varRange will be of type Range.
Last edited by Goshute on 14 May 2010, 22:26, edited 1 time in total.
Goshute
I float in liquid gardens
I float in liquid gardens
-
- Administrator
- Posts: 78592
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands