gpt4 book ai didi

vba - 如何将变体传递给双数组 VBA?

转载 作者:行者123 更新时间:2023-12-04 22:32:01 31 4
gpt4 key购买 nike

我的想法是创建一个函数来传递这样的双数组:

Function pass(a() As Double, b() as double) As Boolean
Dim i As Integer, j As Integer
ReDim a(0 To UBound(b, 1), 0 To UBound(b, 2))
For i = 0 To UBound(a, 1)
For j = 0 To UBound(a, 2)
a(i, j) = b(i, j)
Next
Next
End Function

这样人们就可以使用:
pass(a,b)

然后a = b。
a=b

不起作用..(哦,天哪,我想念matlab ..)

好的,回到问题:

这适用于两个双数组,但如果我得到一个变体,并且当我使用它时,我可以获得一个变体,这不起作用。那么有没有将变量值传递给双数组的解决方案?数据的结构类似于 double 数组,但没有 double() 类型。

最佳答案

为了解决这个问题,我将删除参数的类型。

Sub test()

Dim VarArry(0 To 4, 0 To 4) As Variant
Dim DblArry(0 To 4, 0 To 4) As Double
Dim VarAssing() As Variant
Dim DblAssing() As Double
Dim c As Boolean

VarArry(0, 1) = 34
DblArry(0, 1) = 34

c = pass(VarAssing, VarArry)
Debug.Print (TypeName(VarAssing))
Debug.Print (VarAssing(0, 1))

c = pass(VarAssing, DblArry)
Debug.Print (TypeName(VarAssing))
Debug.Print (VarAssing(0, 1))

c = pass(DblAssing, VarArry)
Debug.Print (TypeName(DblAssing))
Debug.Print (VarAssing(0, 1))

c = pass(DblAssing, DblArry)
Debug.Print (TypeName(DblAssing))
Debug.Print (VarAssing(0, 1))


End Sub

Function pass(a, b) As Boolean
Dim i As Integer, j As Integer
ReDim a(0 To UBound(b, 1), 0 To UBound(b, 2))
For i = 0 To UBound(a, 1)
For j = 0 To UBound(a, 2)
a(i, j) = b(i, j)
Next
Next
End Function

输出如下
Variant()
34
Variant()
34
Double()
34
Double()
34

关于vba - 如何将变体传递给双数组 VBA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51948918/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com