将值分配给2维数组
我想获得一些数据,我input另一个macros到一个二维数组,所以我可以应用一个函数的数据,但不pipe我尝试我不断收到错误。 数据包括string和数字。 我总是可以引用单元格,忘记数组,但这会使函数复杂化。 这是我的代码:
(声明)
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
我试过其他的select,例如用两个'For'循环和LBound
和UBound
来设置内容单元格,但似乎没有任何工作。
在填充时,你不会得到你的错误,而是显示数组。 因为Msgbox
需要一个String参数,所以不可能只是Msgbox
数组。 另一方面,您可以显示特定的位置(例如oDebt(1,1)
)。
如果您想查看其所有内容,请使用debugging模式和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
换言之:通过指定范围来自动维度数组。 如果您需要数字边界,请使用
nrows = UBound(oDebt, 1) ncols = UBound(oDebt, 2)
在这里你可以看到维度的含义,索引1是行,索引2是列。