将数组传递给VBA中的函数 [英] Passing arrays to functions in VBA

查看:210
本文介绍了将数组传递给VBA中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受数组的过程。因此,我可以传递一个像这样的数组:

I have a procedure which accepts arrays. Thus I can pass an array like :

Public Sub MySub (Something as String, ByRef Arr() as Variant)
  'Stuff
End Sub


Public Sub Test()

   Dim Columns() As Variant
   Columns = Array(1, 2, 3, 4, 5, 6)

   MySub "sth", Columns

End Sub

问题在于,当我想像

MySub "sth", Array(1, 2, 3, 4, 5, 6)

MySub "sth", Cvar(Array(1, 2, 3, 4, 5, 6))

我收到一个编译错误,指出类型不匹配:预期为数组或用户定义类型。

I get a compile error saying that type mismatch: Array or user defined type expected.

如何传递内联数组以起作用?

How can I pass an inline array to function?

推荐答案

我使用Excel 2013运行您的VBA代码,没有类型不匹配错误。
例如,带有子项:

I used Excel 2013 to run your VBA code and there's no type mismatch error. For example with the sub:

Public Sub MySub(Something As String, ByRef Arr() As Variant)
    For i = 0 To UBound(Arr)
        Debug.Print Arr(i)
        Next i
    End
End Sub

我在立即窗口(Ctrl + G)中打印出了1,2,3,4,5,6,6,

I get the 1, 2, 3, 4, 5, 6, numbers printed out in the immediate window (Ctrl + G)

这篇关于将数组传递给VBA中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆