Great post, figured I'd add another couple of useful tips re: stuff that threw me off when I first started using arrays.
First, you mentioned that populating a variant array from the worksheet looks like this:
Dim arr As Variant
arr = ThisWorkbook.Sheets("Example").Range("A1:A10")
What people might not have noticed by looking at your example is that the resulting array will always be two-dimensional. This can be confusing in cases that look one dimensional, ie when the range only includes data from one row or column. See what happens when you try to use the UBound function:
'Array of data from 1 column
Dim columnArr As Variant
columnArr = ThisWorkbook.Sheets("Example").Range("A1:A10").Value
'Returns 10
Debug.Print UBound(columnArr)
'Array of data from 1 row
Dim rowArr As Variant
rowArr = ThisWorkbook.Sheets("Example").Range("A1:J1").Value
'Returns 1
Debug.Print UBound(rowArr)
To loop through the rowArr array, you'd need to get the UBound of the second dimension rather than of the entire array:
'Array of data from 1 row
Dim rowArr As Variant
rowArr = ThisWorkbook.Sheets("Example").Range("A1:J1").Value
'Returns 10
Debug.Print UBound(rowArr, 2)
Second, you mentioned the ReDim statement as a way to change the size of an array after its declaration. Another useful feature of ReDim is that it can be used during the array declaration. This is especially useful when the size of your array is determined by a variable:
'Causes compile-time error
Dim exampleA(1 To Selection.Cells.Count) As Double
'Fine, but verbose
Dim exampleB() As Double
Dim cellCount As Long
cellCount = Selection.Cells.Count
ReDim exampleB(1 To cellCount)
'Most concise
ReDim exampleC(1 To Selection.Cells.Count) As Double
What people might not have noticed by looking at your example is that the resulting array will always be two-dimensional.
The reason this might not be obvious-- and the reason I used the examples I did-- is because of this bit from your post:
Dim va as variant, i as long
Va = range(“A1:A10”).value
For i = lbound(va) to ubound(va)
Which uses the LBound/UBound functions on the entire array. If the array had been built from the range A1:J1, it wouldn't have worked.
I think it's mostly a style preference
Not everyone will share your preferences, and they might want to know that this causes an error:
Dim x As Long
x = 5
Dim arr(1 To x) As Variant
But this doesn't:
Dim x As Long
x = 5
ReDim arr(1 To x) As Variant
Personally I prefer to declare variables close to where they're first used. I also find concise code much easier to read (which is annoying since VBA is such a verbose language), and in this case, I wouldn't declare the "cellCount" variable at all unless I needed to use it several times. If I just needed it once or twice (ie for a loop) I'd just get the UBound of the array without worrying about another variable declaration.
I definitely find the concise version of this function to be the most readable:
Function returnFirstXConcise(strArr As Variant, numChars As Long) As Variant
ReDim resultsArr(LBound(strArr) To UBound(strArr)) As Variant
Dim i As Long
For i = LBound(strArr) To UBound(strArr)
resultsArr(i) = Left(strArr(i), numChars)
Next
returnFirstXConcise = resultsArr
End Function
Function returnFirstXVerbose(strArr As Variant, numChars As Long) As Variant
Dim resultsArr() As Variant
Dim arrBase As Long
Dim arrSize As Long
Dim i As Long
arrBase = LBound(strArr)
arrSize = UBound(strArr)
ReDim resultsArr(arrBase To arrSize) As Variant
For i = arrBase To arrSize
resultsArr(i) = Left(strArr(i), numChars)
Next
returnFirstXVerbose = resultsArr
End Function
It's not about your code being wrong. I was adding information for future people that read your post/the comments, some of whom may appreciate knowing the ReDim declaration option. Getting an error when trying to use a variable in a normal array declaration confused me when I first started using arrays. I'd imagine that's not unique to me.
The only reason I elaborated on my own preferences was to give you some idea of why other people might appreciate the info. The example functions were to show a situation in which using ReDim in the array declaration (and avoiding other variable declarations) might make code more readable/clear for some people.
7
u/epicmindwarp 962 Jan 21 '18
Please be mindful of your flairs. Flair changed to Pro Tip.
This is good stuff. I see you haven't come across Split yet?
6
u/KO_Mouse Jan 21 '18
This can't be overstated. Some of my slower macros are now instantaneous after implementing this methodology.
5
u/Selkie_Love 36 Jan 21 '18
I'm not associated with these guys in any way.
I've been studying arrays on my own, and I found https://excelmacromastery.com/excel-vba-array/ to be a great resource/cheat sheet.
2
u/Douchy_McFucknugget Jan 21 '18
Great write-up. Definitely going to check to see if the web scraping macros I’ve written can be improved by better using arrays.
May not be a speed benefit though, since the bottleneck is the web application I’m scraping - but good practice to follow.
2
u/imjms737 59 Jan 21 '18
Great post. Thanks for taking the time to write this up.
2
u/dm_parker0 148 Jan 22 '18
Great post, figured I'd add another couple of useful tips re: stuff that threw me off when I first started using arrays.
First, you mentioned that populating a variant array from the worksheet looks like this:
What people might not have noticed by looking at your example is that the resulting array will always be two-dimensional. This can be confusing in cases that look one dimensional, ie when the range only includes data from one row or column. See what happens when you try to use the UBound function:
To loop through the rowArr array, you'd need to get the UBound of the second dimension rather than of the entire array:
Second, you mentioned the ReDim statement as a way to change the size of an array after its declaration. Another useful feature of ReDim is that it can be used during the array declaration. This is especially useful when the size of your array is determined by a variable:
2
u/iRchickenz 191 Mar 02 '18
This is a great addition to the VBA Essentials series. I hope there's more on the way!
1
u/excel_learner Jan 22 '18
Spotted a spelling mistake :
Should be
Just for consistency :D
-1
u/chef1075 Jan 22 '18
After moving from VBA to GScript, I'll never go back.