2015-10-13 111 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是列。