初始化数组*types不匹配时出错

尝试利用两个数组来遍历数据传输过程,方法是将单元格中的单元格复制并粘贴到新创build的单元格中。 下面的代码仅仅是负责将正确的数据以正确的顺序从一张纸复制并粘贴到新创build的纸上。 尝试初始化数组时,我收到types不匹配。 它发生在第一个数组,但我没有得到第二个数组来testing,所以它可能是错误的。

注意事项: 1)firmLocationColumn的types是long。 2)存储在所述数组中的所有数据意味着表示列号。 它们是无序的,所以我需要按照正确的顺序将它们存储在数组中,以便遍历它们而不是一遍又一遍地写入相同的信息。

让我知道,如果我错过了任何需要解释的东西,我会编辑我的问题:

Private Sub GetSpecificTradeDetails(ByVal masterListRow As Long, ByVal firmLocationColumn As Long, ByVal newExcelConfirmSheet As Worksheet, ByVal newExcelConfirmSheetLastRow As Long) Dim tradesMasterListColumnIndexArray() As Long Dim newExcelConfirmColumnIndexArray() As Long Dim arrayIndexCounter As Long 'Sets array of columns for loop iteration through data sheet tradesMasterListColumnIndexArray() = [1,4,firmLocationColumn,(firmLocationColumn - 1),(firmLocationColumn + 3),15,16,10,11,8,19,18,17,(firmLocationColumn + 4),9,6,2] newExcelConfirmColumnIndexArray() = [1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18] Select Case firmLocationColumn Case 25 'Sets confirm direction to "BUY" newExcelConfirmSheet.Cells((newExcelConfirmSheetLastRow + 1), 6) = "BUY" Case 27 'Sets confirm direction to "SELL" newExcelConfirmSheet.Cells((newExcelConfirmSheetLastRow + 1), 6) = "SELL" End Select 'Transfers trade details between the masterlist and the newly created confirm sheet With TradesMasterSheet For arrayIndexCounter = 0 To 17 .Cells(masterListRow, tradesMasterListColumnIndexArray(arrayIndexCounter)).Copy _ Destination:=newExcelConfirmSheet.Cells((newExcelConfirmSheetLastRow + 1), newExcelConfirmColumnIndexArray(arrayIndexCounter)) Next End With End Sub 

VBA不支持通过数组文字初始化数组。 它确实有一个Array()函数:

 Dim tradesMasterListColumnIndexArray As Variant Dim newExcelConfirmColumnIndexArray As variant tradesMasterListColumnIndexArray = Array(1,4,firmLocationColumn,(firmLocationColumn - 1),(firmLocationColumn + 3),15,16,10,11,8,19,18,17,(firmLocationColumn + 4),9,6,2) newExcelConfirmColumnIndexArray = Array(1,2,3,4,5,7,8,9,10,11,12,13,14,15,16,17,18)