自定义函数中的语法错误

时间:2015-08-04 16:47:30

标签: arrays excel vba function parameters

我在以下代码的第一行中遇到语法错误。我正在使用array function parameters as shown in MSDN。从String()中删除括号后,错误消失,但我需要一个字符串数组。 到目前为止我的代码:

Private Function Contains(name As String, names As String()) As Boolean
Contains = False

Dim Index As Integer
For Index = 0 To names.GetUpperBound(0)
    If names(Index) = name Then
        Contains = True
        Exit For
    End If
Next
End Function

2 个答案:

答案 0 :(得分:0)

试试这个。

Private Function Contains(name As String, names() As String) As Boolean

Private Function Contains(name As String, ByRef names() As String) As Boolean

取决于您的需求。

答案 1 :(得分:0)

数组的括号应该在变量名之后,而不是在类型之后:

names() as String

而不是

names as String()

BTW:namenames不是好用的变量名!最好是更具说服力。