2015-10-13 210 views
0

我試圖獲得與其他宏觀一些數據我輸入到2維數組,所以我可以再申請一個功能,即數據,但無論我怎麼努力我不斷收到錯誤。數據包括字符串和數字。我總是可以參考單元格並忘記數組,但這會使函數複雜化。這裏是我的代碼:分配值2維數組

(聲明)

Dim nLiens As Byte, nCreditors As Byte 
Dim SecurityV As Currency, ASecurityV As Currency 
Const adjuster = 0.9 

(相關潛艇)

Public Sub VariableDeclaration() 
    nLiens = InputBox("Enter number of liens in security") 
    nCreditors = InputBox("Enter number of creditors") 
    SecurityV = InputBox("Enter security full value") 
    ASecurityV = adjuster * SecurityV 
Call ODebt 
End Sub 

Sub ODebt() 

' 
'(...) 
' 

Dim oDebt() As Variant 
ReDim oDebt(1 To nCreditors + 1, 1 To nLiens + 1) 
Dim rg As Range 
Set rg = Range(Cells(1, 1), Cells(nCreditors + 1, nLiens + 1)) 
oDebt = rg.Value 

MsgBox (oDebt) 
'>>> ERROR: type mismatch 


Call SAllocation 
End Sub 

我已經嘗試過其他的替代品,比如有兩個設置由單元格的內容細胞「對於」循環和LBoundUBound,但似乎沒有任何工作。

回答

1

你得到你的錯誤不卻使,但在顯示陣列。 它不可能只是Msgbox陣列,因爲Msgbox期望一個字符串參數。另一方面,您可以顯示特定位置(例如oDebt(1,1))。

如果你想看看它的所有內容,無論是使用調試模式和Local窗口,或將其打印到一些未使用的電池。

Dim oDebt As Variant 
Dim rg As Range 

Set rg = Range(Cells(1, 1), Cells(nCreditors + 1, nLiens + 1)) 
oDebt = rg ' get data from sheet 
'... do calculations with oDebt array 
rg = oDebt ' put data on sheet 

在口頭上:

+0

我明白了!謝謝!! – Migarisa

+0

如果這回答你的問題,請將其標記爲未來讀者解決! – Verzweifler

0

我會從數據表中這樣的值複製您自動維度數組通過指定的範圍內。如果您需要數字邊界,請使用

nrows = UBound(oDebt, 1) 
ncols = UBound(oDebt, 2) 

這裏您還可以看到維度的含義,索引1是行,索引2是列。