Search code examples
excelvbafunctionreturnreturn-value

How do I return more than one result from a function vba excel?


How do I return more than one result from a function vba?

Public Function test() As Integer
    test = 1, 2, 3
End Function

This gives a compile error.

How do I make this function return an integer?


Solution

  • I would use an array in this case

    Option Explicit   
    Public Sub PrintTest()
        Dim result() As Variant, i As Long
    
        result = test
    
        For i = LBound(result) To UBound(result)
            Debug.Print (result(i))
        Next
    
    End Sub
    
    Public Function test() As Variant
        test = Array(1, 2, 3)
    End Function