refactor
refactor

Reputation: 15104

"Can't assign to array" compile error

I am new to VBA , When I execute below function , am getting "Can't assign to array" compile error

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

My requirement is to call above function and assign the array returned by function to a variable.

My code for calling above function is

Dim x() As String    
x = GetPriorities

Upvotes: 2

Views: 6660

Answers (1)

Florent B.
Florent B.

Reputation: 42538

You need to declare the array so it can match the returned type:

Function GetPriorities() As String()

    Dim wksMapping As Worksheet
    Dim arrHighPriority() As String

    ...

    GetPriorities = arrHighPriority

End Function

Upvotes: 2

Related Questions