[英]Error Initializing Array *Type Mismatch*
嘗試通過將單元格中的單元格復制並粘貼到新創建的單元格中來利用兩個數組來迭代數據傳輸過程。 下面的代碼僅負責以正確的順序從正確的順序復制和粘貼正確的數據到新創建的數據。 嘗試初始化數組時,我收到類型不匹配。 它發生在第一個數組上,但我還沒有到第二個數組進行測試,所以它也可能是錯誤的。
注意事項: 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
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.