"无法分配到数组"编译错误

时间:2016-03-31 08:15:40

标签: vba

我是VBA的新手,当我执行以下功能时,我得到"无法分配给数组"编译错误

Function GetPriorities() As String()

    Dim wksMapping As Worksheet
    Dim LastRowMapping As Long
    Dim cnt As Long
    Dim cntIndex As Long

    cntIndex = 0
    LastRowMapping = Worksheets(MAPPING).Cells(Worksheets(MAPPING).Rows.Count, "A").End(xlUp).Row
ReDim arrHighPriority(LastRowMapping - 2)

    For cnt = 2 To LastRowMapping
      arrHighPriority(cntIndex) = Worksheets(MAPPING).Cells(cnt, 2).Value
      cntIndex = cntIndex + 1
    Next cnt

    GetPriorities = arrHighPriority

End Function

我的要求是调用上面的函数并将函数返回的数组赋值给变量。

我调用上述函数的代码是

Dim x() As String    
x = GetPriorities

1 个答案:

答案 0 :(得分:2)

您需要声明数组,使其与返回的类型匹配:

Function GetPriorities() As String()

    Dim wksMapping As Worksheet
    Dim arrHighPriority() As String

    ...

    GetPriorities = arrHighPriority

End Function