初始化阵列时出错*类型不匹配*

时间:2016-04-27 13:29:07

标签: arrays excel vba excel-vba

尝试通过将单元格中的单元格复制并粘贴到新创建的单元格中来利用两个数组来迭代数据传输过程。下面的代码仅负责以正确的顺序从正确的顺序复制和粘贴正确的数据到新创建的数据。尝试初始化阵列时,我收到类型不匹配。它发生在第一个数组上,但我还没有到第二个数组进行测试,所以它也可能是错误的。

注意事项: 1)firmLocationColumn类型为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

1 个答案:

答案 0 :(得分:3)

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)