vba - "Can' t 分配给数组”编译错误

标签 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

最佳答案

您需要声明数组,以便它可以匹配返回的类型:

Function GetPriorities() As String()

    Dim wksMapping As Worksheet
    Dim arrHighPriority() As String

    ...

    GetPriorities = arrHighPriority

End Function

关于vba - "Can' t 分配给数组”编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327755/

相关文章:

VBA Dir 函数不适用于 Excel 2010

excel - 使用 Excel VBA 关闭 Powerpoint 对象(不使用 Powerpoint.Application)

excel - VBA WinHTTP 登录到 Oddsportal

python - 如何使用 xlwings 从 Python 调用 Excel 宏?

sql - 在 Excel 中针对 Access DB 中的空数据生成 !N/A 错误

ms-access - 传递数组 byref 不会编辑原始数组

excel - 在 Range.Find VBA 中使用 Or 函数

vba - 结束如果没有阻止如果错误VBA

html - 从类中提取跨度值,VBA

vba - 组合框中的今天日期和 future 日期下拉列表