如何将数组传递给函数

时间:2019-04-24 03:53:14

标签: excel vba

我尝试将数组传递给VBA EXCEL。我在尝试时找不到合适的例子

我尝试将数组传递给函数 在函数声明中 我尝试将变量数组设置为变体或ByRef但不起作用 我怎么了?

Dim Retarray(1 To 6, 1 To 2) As String  

这是我的数组 然后尝试传递为

Smap = checkDevNo(Retarray, Right(CellArray(i, 1), 1))

这是我的函数声明

Function checkDevNo(ByRef aaray() As String, aa As Character) As String

Select Case aa

Case "1"
    For j = 1 To 6
        If aaray(j, 1) = 6 Then
            checkDevNo = j
        End If
    Next j
Case "2"
    For j = 1 To 6
        If aaray(j, 1) = 7 Then

        End If
    Next j
End Select
End Function

编译错误用户定义类型未定义??

2 个答案:

答案 0 :(得分:1)

IIRC,Character不是VBA类型。您应该将其传递为StringByte。在Visual Basic中,单个字符也可以存储在定长字符串(Dim myChar As String * 1)中,但是方法参数不支持该语法。

所以您的函数声明应如下所示:

Function checkDevNo(ByRef aaray() As String, aa As String) As String
    ...
End Function

答案 1 :(得分:0)

如果我们正在谈论Excel VBA,则需要执行以下操作:

Function checkDevNo(aaray As Variant, aa As String) As String

    Select Case aa

        Case "1"
            For j = 1 To 6
                If aaray(j, 1) = 6 Then
                    checkDevNo = j
                End If
            Next j
        Case "2"
            For j = 1 To 6
                If aaray(j, 1) = 7 Then

                End If
            Next j

    End Select

End Function