Mackeral
Board Regular
- Joined
- Mar 7, 2015
- Messages
- 249
- Office Version
- 365
- Platform
- Windows
Here's the code:
The the tricky thing about accsssing the data is the Row and Col data are interchanged, so to get the data from a Row/Column, you have to access it like this:
The reason it had to be coded this way is because the second argument in the ReDim statement is the only one you can change on the fly. And it took a bit of scrounging about to find that this was the way it worked.
I developed this because I needed to have data data from a file sorted in a different way that the file was ordinarily organized. So I sorted by what I was interested in, made an array of what I wanted in that order, and then resorted the file back into it's most useful order.
VBA Code:
Function Make_Variant_Array(SHEET As Worksheet, ParamArray Col_Nrs()) As Variant
' Return an Array from a SHEET for each row containing the data from particular "Columns".
' 8/29/20 Created. Mac Lingo
' Note: Since you can only resize the last dimension of the ReDim statement, the Rows and Columns get reversed.
Dim Args() As Variant
Sheet_Name = SHEET.Name
Dim Columns(1 To 10) As Integer
Col_Knt = 0
For Each Value In Col_Nrs
Col_Knt = Col_Knt + 1
Columns(Col_Knt) = Value
Next
ReDim Preserve Args(Col_Knt, 0)
Args(0, 0) = 1: Args(1, 0) = 1: Args(2, 0) = 2
LastRow = Last_Row(SHEET)
For Row = 1 To LastRow
ReDim Preserve Args(Col_Knt, Row)
For Col = 1 To Col_Knt
Args(Col, Row) = SHEET.Cells(Row, Columns(Col))
Next
Row_Knt = Row_Knt + 1
Next Row
Args(1, 0) = LastRow
Make_Variant_Array = Args
End Function ' Make_Variant_Array
The the tricky thing about accsssing the data is the Row and Col data are interchanged, so to get the data from a Row/Column, you have to access it like this:
VBA Code:
TS = Variant_Data(Column, Row)
The reason it had to be coded this way is because the second argument in the ReDim statement is the only one you can change on the fly. And it took a bit of scrounging about to find that this was the way it worked.
I developed this because I needed to have data data from a file sorted in a different way that the file was ordinarily organized. So I sorted by what I was interested in, made an array of what I wanted in that order, and then resorted the file back into it's most useful order.