locked
Passing data between subroutines and functions RRS feed

  • Question

  • Can you pass 3 variable into a function and return 2 variables back?

    Public mo as double
    Public dy as double
    Public yr as double
    Public j as double
    Public jd as double
    Sub First()
         mo=...
         dy=...
         yr=...
         function1(mo,dy,yr)
    
    end sub
    
    function function1(Byval mo as double, Byval dy as double, Byval yr as double)
       j=...
       jd=...
       Return(j,jd)
    end function
    Tuesday, March 30, 2010 2:20 AM

Answers

  • SJoo has a good suggestion. Change his code to this:

    Function function1(ByVal mo As Double, ByVal dy As Double, ByVal yr As Double) As Variant()

        Dim arReturn() As Variant
       arReturn = Array(mo, dy, yr)
       function1 = arReturn
    End Function

    Nadia

    • Proposed as answer by NadiaSz Wednesday, March 31, 2010 3:58 AM
    • Marked as answer by Tim Li Thursday, April 1, 2010 8:27 AM
    Tuesday, March 30, 2010 6:56 PM

All replies

  • Hello,

    You can concatenate return values to a string and then parse it, or you can use Type structure.

    Nadia

    Tuesday, March 30, 2010 4:10 AM
  • Hi

    Function returns only one variable.

    You can return several values using Array as following:

    Sub t()
        Dim a As Double
        Dim b As Double
        Dim c As Double
        Dim d
        Dim i As Long
        
        a = 3
        b = 4
        c = 5
        
        d = function1(a, b, c)
        
        For i = 0 To UBound(d)
            Debug.Print d(i)
        Next
    End Sub
    
    Function function1(ByVal mo As Double, ByVal dy As Double, ByVal yr As Double)
        function1 = Array(mo, dy, yr)
    End Function

    HTH

    SJOO


    the best time to plant a tree was twenty years ago. the second best time, is today (Chinese proverb) sjoo.kwak at gmail.com
    Tuesday, March 30, 2010 6:44 AM
  • Keep getting a "Type mismatch' error.  Tried "cdbl" it does not work.

    Sub t()
        Dim a As Double
        Dim b As Double
        Dim c As Double
        Dim d
        Dim i As Long
       
        a = 3
        b = 4
        c = 5
       
        d = function1(a, b, c) 'get "Type mismatch' error
        
        For i = 0 To 4
            Debug.Print d(i)
        Next
    End Sub

    Function function1(ByVal mo As Double, ByVal dy As Double, ByVal yr As Double)

         function1 = Array(mo, dy, yr) 'get "Type mismatch' error

    End Function

    'tried "Return(d)" that resulted in a "Syntax" error.

    Tuesday, March 30, 2010 12:09 PM
  • SJoo has a good suggestion. Change his code to this:

    Function function1(ByVal mo As Double, ByVal dy As Double, ByVal yr As Double) As Variant()

        Dim arReturn() As Variant
       arReturn = Array(mo, dy, yr)
       function1 = arReturn
    End Function

    Nadia

    • Proposed as answer by NadiaSz Wednesday, March 31, 2010 3:58 AM
    • Marked as answer by Tim Li Thursday, April 1, 2010 8:27 AM
    Tuesday, March 30, 2010 6:56 PM
  • Nadia,  It works great, thanx.

    Tuesday, March 30, 2010 8:17 PM
  • You are welcome,

    Nadia

    Wednesday, March 31, 2010 3:58 AM