What my sub is trying to do:
Take an array (e.g. (1 to 100, 1 to 36), contained in an array of arrays)
Take only some of the columns (e.g. I only want columns 2,5,7 etc.)
Replace the original array with one containing only those columns (so it will go from (1 to 100, 1 to 36) to e.g. (1 to 100, 1 to 5)).
I do this by copying the columns I want to a new array, then erasing the original and re-creating it with only the new information.
N.B. all my arrays are dimmed as variants first dim arrVariable as Variant with arrVariable = Array()
I have an array of arrays arrAggregatedArrays(1 to 8)
The relevant part of my sub goes:
FilterSheetArrayForColumns (ArrAggregatedArrays(i))
Private Sub FilterSheetArrayForColumns(ByRef arrSource As Variant)
'/======================================================================================================================================================
'/ Author: Zak Armstrong
'/ Email: [email protected]
'/ Date: 12/August/2015
'/
'/ Description: Takes Sheet arrays, finds the columns from the colAllHeadings, recreates the array with just that data (and empty columns for the ones not found)
'/======================================================================================================================================================
Dim i As Long
Dim j As Long
Dim k As Long
Dim lngFinalRow As Long
Dim lngFinalColumn As Long
Dim arrTempArray As Variant '/ Temporarily holds the filtered information
arrTempArray = Array()
Dim arrHeadingsRow As Variant '/ Holds the top (headings) row for application.match
arrHeadingsRow = Array()
Dim varColumnPosition As Variant '/ Holds the position of the relevant column
Dim strHeading As String '/ The current heading to search for
'/======================================================================================================================================================
AssignArrayBounds arrSource, UB1:=lngFinalRow, UB2:=lngFinalColumn
'/==================================================
'/ Recreate Headings Row
'/==================================================
ReDim arrHeadingsRow(1 To lngFinalColumn)
For i = 1 To lngFinalColumn
arrHeadingsRow(i) = arrSource(1, i)
Next i
'/==================================================
'/ Find Columns, put in array
'/==================================================
ReDim arrTempArray(0 To lngFinalRow, 0 To ColAllHeadings.Count)
arrTempArray(0, 0) = arrSource(0, 0)
Dim lngDestinationColumn As Long
Dim lngSourceColumn As Long
For i = 1 To ColAllHeadings.Count
strHeading = ColAllHeadings(i)
varColumnPosition = Application.Match(strHeading, arrHeadingsRow, 0)
If IsError(varColumnPosition) _
Then
MissingDataHeadingsHandler arrSource, strHeading
Else
lngDestinationColumn = i
lngSourceColumn = varColumnPosition
CopyArrayColumn2d arrSource, arrTempArray, lngSourceColumn, lngDestinationColumn
End If
Next i
CopyArrayContents2d arrTempArray, arrSource
End Sub
But, at the end of this sub, arrAggregatedArrays(i) still contains the original array, not the filtered one.
I imagine my code might just be erasing the reference to the array, as opposed to the array itself. If that's the case, how do I reference it properly?
If that's not the case, where is the variable referencing going wrong?