We help IT Professionals succeed at work.
Get Started

VBA Excel: How to return function as an array

Last Modified: 2012-05-09
Hi X-perts,

I need to return function as an array and assign it to another array.

What is wrong with my code?

Please, advise

Public Function qtest(ByVal n As Integer) As Variant
    Dim res(0 To 3) As Variant
    res(0) = 2.12 * n
    res(1) = 3.14 + n
    res(2) = 1.18
    res(3) = 2.12
    qtest = res
End Function

Public Sub aaa()
    Dim fnc(0 To 3) As Variant
    fnc = qtest(3) '==== This line returns error "Cannot assign to array"
    zz = fnc(0)
    zz = fnc(1)
    zz = fnc(2)
End Sub

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE