将不同类型的数组传递给UDF函数

时间:2013-06-28 21:12:05

标签: excel excel-vba vba

我有一个在Excel工作表中显示数组内容的函数。我的函数的标题如下所示:

Public Sub afficher_signal(ByRef signal() As Integer, ByVal nb_ligne As Integer)

问题是有时候我想用它来显示一个Integer数组,有时我想要显示一个双数组。有没有办法改变 ByRef signal() as Integer 通过接受任何类型数组的东西?

非常感谢!

1 个答案:

答案 0 :(得分:1)

替换

Public Sub afficher_signal(ByRef signal() As Integer, ByVal nb_ligne As Integer)

Public Sub afficher_signal(ByRef signal, ByVal nb_ligne As Integer)

Public Sub afficher_signal(ByRef signal as Variant, ByVal nb_ligne As Integer)


下面演示了afficher_signal

的示例用法
Public Sub Main()
    Dim arr As Variant
    arr = Array(1, "yo", 3, 10 Mod 2, 5.25)
    afficher_signal arr, 0
    afficher_signal2 arr, 0
End Sub

Public Sub afficher_signal(ByRef signal As Variant, ByVal nb_ligne As Integer)
    Dim i As Long
    For i = LBound(signal) To UBound(signal)
        Debug.Print signal(i)
    Next i
End Sub

Public Sub afficher_signal2(ByRef signal, ByVal nb_ligne As Integer)
    Dim i As Long
    For i = LBound(signal) To UBound(signal)
        Debug.Print signal(i)
    Next i
End Sub