我的潜水员试图做什么:
取一个阵列(例如(1到100,1到36),包含在数组阵列中)
只取一些列(例如我只想要列2,5,7等)
将原始数组替换为仅包含那些列的数组(因此它将从(1到100,1到36)变为例如(1到100,1到5))。
我这样做是通过将我想要的列复制到一个新数组,然后擦除原始数据并仅使用新信息重新创建它。
N.B。我的所有数组都变为dim arrVariable as Variant
变体arrVariable = Array()
我有一个数组数组arrAggregatedArrays(1 to 8)
我子的相关部分是:
FilterSheetArrayForColumns (ArrAggregatedArrays(i))
Private Sub FilterSheetArrayForColumns(ByRef arrSource As Variant)
'/======================================================================================================================================================
'/ Author: Zak Armstrong
'/ Email: zak.armstrong@luminwealth.co.uk
'/ 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
但是,在此子句的末尾,arrAggregatedArrays(i)
仍包含原始数组,而不是已过滤的数组。
我想我的代码可能只是删除了对数组的引用,而不是数组本身。如果是这种情况,我该如何正确引用它?
如果不是这样,变量引用哪里出错?
答案 0 :(得分:1)
函数FilterSheetArray()未使用参数“arrSource”
试试这个:
FilterSheetArray(arrAggregatedArrays(i))
Sub FilterSheetArray(ByRef arrSource as variant)
dim arrTemp as variant
arrTemp = arrSource ' <----------------------
'/ fill arrTemp with specific columns from arrSource
'/ Erase arrSource, ReDim and copy contents of arrTemp
arrSource = arrTemp
End Sub
并确保在末尾将“arrTemp”分配回“arrSource”
(或者只使用“arrSource”并删除“arrTemp”)
答案 1 :(得分:0)
我发现了问题。我使用这种语法调用我的sub:
FilterSheetArrayForColumns (ArrAggregatedArrays(i))
当我应该使用时:
FilterSheetArrayForColumns ArrAggregatedArrays(i)
在VBA中,只要在变量周围使用括号,它就会强制VBA对其进行评估。因此,在这种情况下,它没有传递变量arrAggregatedArrays(i),而是传递评估(实际上是本地副本)。
所以sub正在做它应该做的事情,但是它没有引用原始变量,而只是改变了一个本地副本,一旦副结束就消失了,原来没有变化。