I'm presently populating my array Securities with the following code:
Option Base 1
Securities = Array(Worksheets(3).Range("A8:A" & SymbolCount).Value)
This produces a 2-dimensional array where every address is (1...1,1...N). I want a 1-dimensional array (1...N).
How can I either (a) populate Securities as a 1-dimensional array, or, (b) efficiently strip Securities to a 1-dimensional array (I'm stuck at a with each loop).
This will reflect the answer iDevlop gave, but I wanted to give you some additional information on what it does.
Probably the fastest way to get a 1D array from a range is to dump the range into a 2D array and convert it to a 1D array. This is done by declaring a second variant and using
ReDim
to re-size it to the appropriate size once you dump the range into the first variant (note you don't need to use Array(), you can do it as I have above, which is more clear).The you just loop through the 2D array placing each element in the 1D array.
I hope this helps.
I know you already accepted an answer but here is simpler code for you:
If you are grabbing a singe row (with multiple columns) then use:
If you are grabbing a single column (with multiple rows) then use:
So, basically you just transpose twice for rows and once for columns.
Update:
Large tables might not work for this solution (as noted in the comment below):
Update 2:
Another problem you might have as mentioned in the comments:
It has been a long time since I worked with Excel VBA but this might be a general limitation of accessing the data this way?
If you read values from a single column into an array as you have it then I do think you will end up with an array that needs to be accessed using
array(1, n)
syntax.Alternatively, you can loop through all cells in your data and add them into an array:
It is possible by nesting Split/Join and Transpose to create an array of String from the Range. I haven't yet tested performance against a loop, but it's definitely a single pass.
This code takes a Range (my sample was 1 column wide, with 100 rows of "abcdefg"), Transposes it to make convert it to a single dimension, JOINs the String array, using vbTab as a separator, then Splits the joined string on the vbTab.
It is limited to string Arrays, as Join and Split are both String functions. Numbers would require manipulation.
EDIT 20160418 15:09 GMT
Test using two methods, writing to Array by loop and using Split/Join/Transpose 100 rows, 10k, 100k, 1mil
Rows Loop SplitJoinTranspose
100 0.00 0.00
10000 0.03 0.02
100000 0.35 0.11
1000000 3.29 0.86
EDIT 20160418 15:49 GMT Added function TwoDtoOneD function and results
Rows Loop SplitJoinTranspose TwoDtoOneD
100 0.00 0.00 0.00
10000 0.03 0.02 0.01
100000 0.34 0.12 0.11
1000000 3.46 0.79 0.81
EDIT 20160420 01:01 GMT
The following are the Sub and function I used to conduct my tests
EDIT 20160420 12:48 GMT
As @chris neilsen indicated, there's definitely a flaw in my tests. Seems the Array for Split/Join/Transpose is not taking more than 16k rows, which is still under the 65k limit he indicated. This, I'll admit, is a surprise to me. My tests were definitely incomplete and flawed.