将值分配给二维数组

时间:2015-10-13 08:32:01

标签: excel vba excel-vba multidimensional-array

我正在尝试将我用另一个宏输入的一些数据转换为二维数组,这样我就可以将函数应用于该数据,但无论我尝试什么,我都会遇到错误。数据包括字符串和数字。我总是可以引用单元格而忘记数组,但这会使函数复杂化。这是我的代码:

(声明)

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

我尝试了其他替代方法,例如使用两个“For”循环和LBound以及UBound逐个单元格设置内容,但似乎没有任何效果。

2 个答案:

答案 0 :(得分:1)

您在填充时没有收到错误,但是在显示数组时。 仅Msgbox数组是不可能的,因为Msgbox需要一个String参数。另一方面,您可以显示特定位置(例如oDebt(1,1))。

如果您想查看其所有内容,请使用调试模式和Local窗口,或将其打印到一些未使用的单元格。

答案 1 :(得分:0)

我会以这种方式复制数据表中的值:

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 

单词:通过指定范围自动标注数组。如果需要数字边界,请使用

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

在这里你也可以看到维度的含义,索引1是行,索引2是列。

相关问题