gpt4 book ai didi

vba - 将数组传递给 ParamArray

转载 作者:行者123 更新时间:2023-12-03 13:26:58 25 4
gpt4 key购买 nike

是否可以将数组的所有元素传递给 ParamArray?

例如,我想将一个 ParamArray 传递给另一个 ParamArray:

Sub test()
p1 "test", "banane", "birne"
End Sub

Sub p1(ParamArray keys() As Variant)
p2 keys 'should be the same as: p2 "test", "banane", "birne"
End Sub

Sub p2(ParamArray keys() As Variant)
Dim key As Variant
For Each key In keys
Debug.Print key 'Run-time error '13' Type mismatch (key is an array)
Next key
End Sub

在这种情况下, p2 的 ParamArray不包含 keys 的元素, 但它得到数组对象 keys .因此,我必须检查是否传递了数组:
Sub test()
p1 "test", "banane", "birne"
p2 "test", "banane", "birne"
End Sub

Sub p1(ParamArray keys() As Variant)
p2 keys
End Sub

Sub p2(ParamArray params() As Variant)
Dim keys As Variant
If IsArray(params(0)) Then
keys = params(0)
Else
keys = params
End If

Dim key As Variant
For Each key In keys
Debug.Print key
Next key
End Sub

但这与 Java 相比是很尴尬的:
public class VarArgs {

public static void main(String[] args) {
p1("test", "banane", "birne");
p2("test", "banane", "birne");

String[] array = {"test", "banane", "birne"};
p1(array);
p2(array);
}

public static void p1(String... strings) {
p2(strings);
}

public static void p2(String... strings) {
for (String string : strings) {
System.out.println(string);
}
}

}

在Java中我不必区分。但这在 VBA 中可能是不可能的。

感谢帮助,
迈克尔

最佳答案

将 ParamArray 参数传递给另一个需要 ParamArray 参数的函数(委托(delegate) ParamArray 参数)。
我需要委托(delegate)给以下类型的函数:strf(str as string, ParamArray args() as Variant) as String在 ParamArray 中的其他函数中接收到的参数直接传递而无需显式写入。
我发现的限制是:

  • ParamArray() 它只能传递给另一个需要 ParamArray 的函数。
  • ParamArray 在元素 0 处作为 Variant ()
  • 接收
  • 当第二个函数接收到它时,它会增加一个深度
    我还没有找到任何令人满意的解决方案,但我编写了一个完美运行的函数,撤消添加的深度级别并返回一个带有接收参数的向量。

  • 代码:
    Option Explicit
    Option Base 1

    Public Sub PrAr1(ParamArray pa1() As Variant)
    Dim arr() As Variant
    arr = fn.ParamArrayDelegated(pa1)
    PrAr2 pa1
    End Sub

    Public Sub PrAr2(ParamArray pa2() As Variant)
    Dim i As Integer, arrPrms() As Variant
    arrPrms = fn.ParamArrayDelegated(pa2)
    For i = 0 To UBound(arrPrms)
    Debug.Print s.strf("i: %0 prm: %1 ", i, arrPrms(i))
    Next i
    PrAr3 pa2
    End Sub

    Public Sub PrAr3(ParamArray pa3() As Variant)
    Dim i As Integer, arrPrms() As Variant
    arrPrms = fn.ParamArrayDelegated(pa3)
    For i = 0 To UBound(arrPrms)
    Debug.Print s.strf("i: %0 prm: %1 ", i, arrPrms(i))
    Next i
    End Sub

    Public Function ParamArrayDelegated(ParamArray prms() As Variant) As Variant
    Dim arrPrms() As Variant, arrWrk() As Variant
    'When prms(0) is Array, supposed is delegated from another function
    arrPrms = prms
    Do While VarType(arrPrms(0)) >= vbArray And UBound(arrPrms) < 1
    arrWrk = arrPrms(0)
    arrPrms = arrWrk
    Loop
    ParamArrayDelegated = arrPrms
    End Function

    关于vba - 将数组传递给 ParamArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20783170/

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