+ Reply to Thread
Results 1 to 7 of 7

Passing variables

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2006
    Posts
    80

    Passing variables

    I have a question. Can this be done. Pass a couple of varibles back from a called sub routine?

    The calling sub calls the called sub does which has code plus a couple of varibles needing to come back.

    Scope: The called sub is a series of case statements with code and sets varibles which are needed back in the calling sub.

    Thanks,

    Ralph
    Last edited by Zrx; 11-04-2009 at 07:46 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: varible pass question

    Just like you can pass variables into a function in a cell (for instance a LOOKUP() formula has two or three parameters), you can pass a variable into a subroutine the same way.

    Sub TestMe(Var1 as String) as Boolean
        If IsNumeric(Var1) Then TestMe = True
    End Sub
    This sub expects to see an value for the Var1 arrive with the call. So another sub could be looking for another variable "n" like so:

    n = TestMe(Range("A1"))
    So that passes the value in cell A1 to the "testme" routine and gets a Boolean True/False answer back as the value of "n".

    Is that the kind of thing you're talking about?


    ========
    Alternately, you can declare a variable as PUBLIC so that the value in the variable survives from one sub to the next.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: varible pass question

    That doesn't compile, JB.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: varible pass question

    Quote Originally Posted by shg View Post
    That doesn't compile, JB.
    *groan**complain*...

    That's what I get for posting "air code"... but the idea is still correct even if my syntax is off in the example...which probably is useless in the real need of the OP anyway.

    If you post some actual examples of what you're trying to do, I can help construct that actually works... (shame)

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Passing variables

    I think in this case JB it's a case of routine 1 calling routine2 -> routine 2 generates some variables prior to completion -> upon completion routine 1 can now utilise those variables generated by routine2.

    What is the nature of the 2nd routine ? Sounds to me like you should be using a Function - and calling the Function from Routine 1 ... eg

    Sub RoutineOne()
    Dim lngX As Long, strY As String
    lngX = LongFunction(4,2)
    strY = StringFunction("m","Donuts")
    MsgBox "lngX = " & lngX & vblf & "strY = " & strY
    End Sub
    
    Function LongFunction(lngOne As Long, lngTwo As Long) As Long
    LongFunction = lngOne Mod lngTwo
    End Function
    
    Function StringFunction(strOne As String, strTwo As String) As String
    StringFunction = String(5,strOne) & " " & strTwo & "!"
    End Function
    Bad example but you get the general gist.

    If you do really need to use variables then you can (as already mentioned by JB) think about creating the variables as Public (see: http://www.ozgrid.com/VBA/variable-scope-lifetime.htm)

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Passing variables

    I have a different take on the OP's question.

    You can pass variables ByRef or ByVal and have them updated and returned in the called routine.

    Sub Main()
        
        Dim strA As String
        Dim lngB As Long
        
        
        strA = "Hello "
        lngB = 5
        MsgBox strA & " " & lngB, , "Before Call"
        
        Call MySubByRef(strA, lngB)
        
        MsgBox strA & " " & lngB, , "After Call using ByRef"
        
        Call MySubByVal(strA, lngB)
        
        MsgBox strA & " " & lngB, , "After Call using ByVal"
        
        
    End Sub
    
    Sub MySubByVal(ByVal Text As String, ByVal Number As Long)
    
        Text = "Goodbye "
        Number = Number * 5
        
    End Sub
    Sub MySubByRef(Text As String, Number As Long)
    
        Text = "Goodbye "
        Number = Number * 5
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  7. #7
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Passing variables

    That doesn't compile, JB.
    I thought it was just me. Phew! What a relief.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1