Mar*_*ark 2 excel vba excel-vba user-defined-types
我有一个用户定义的类型,决定:
Public Type Decision
choice As String
cost As Double
End Type
Run Code Online (Sandbox Code Playgroud)
我试图使用我的UDT数组来存储动态程序的结果(阶段/状态的选择和成本)。
Public Function DPSolve(arg1, arg2, ...) as Decision
Dim Table() As Decision
ReDim Table(arg1, arg2+ 1)
'do stuff that fills each Table().choice and Table().cost
'return Table()
DPSolve = Table()
End Function
Run Code Online (Sandbox Code Playgroud)
如果我想将此函数的结果传递给新函数(也就是说,在Excel中打印Table(),或者使用Table()结果做更多工作,该怎么做?
我在尝试
Sub Main
Dim x as variant
x = DPSolve(arg1, arg2, ...)
Function2(x)
End Main
Run Code Online (Sandbox Code Playgroud)
我曾尝试使x成为数组,但出现“无法分配给数组”错误。我也尝试过做出xa决策,但是那也不起作用。该代码在模块中。
谢谢!
因此DPSolve
应返回Decision
s 的数组。并且x()
也应该是Decision
s 的数组。
Public Type Decision
choice As String
cost As Double
End Type
Public Function DPSolve(arg1, arg2) As Decision()
Dim Table() As Decision
ReDim Table(arg1, arg2 + 1)
'do stuff that fills each Table().choice and Table().cost
'return Table()
DPSolve = Table()
End Function
Sub Main()
Dim x() As Decision
x = DPSolve(2, 2)
End Sub
Run Code Online (Sandbox Code Playgroud)
为我工作。例:
Public Type Decision
choice As String
cost As Double
End Type
Public Function DPSolve(arg1, arg2) As Decision()
Dim Table() As Decision
ReDim Table(arg1, arg2 + 1)
'do stuff that fills each Table().choice and Table().cost
Table(1, 2).choice = "choice1,2"
Table(1, 2).cost = 123.45
'return Table()
DPSolve = Table()
End Function
Sub Main()
Dim x() As Decision
x = DPSolve(2, 2)
MsgBox x(1, 2).choice
MsgBox x(1, 2).cost
End Sub
Run Code Online (Sandbox Code Playgroud)
通过“无法分配给数组”来明确说明。您不能将类型和大小标注并填充的数组分配给另一个类型和大小标注的数组。但是,您当然可以将填充数组分配给具有尺寸标注但没有尺寸标注的数组。
Sub test()
Dim arr1(3) As String
Dim arr2() As String
arr1(0) = "Value 0"
arr1(1) = "Value 1"
arr1(2) = "Value 2"
arr1(3) = "Value 3"
arr2 = arr1
MsgBox Join(arr2, ", ")
End Sub
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3951 次 |
最近记录: |