Passing Variables Between Subs

  • This is my first question. I am having trouble passing a variable between two subs. The variable in the second sub is always zero.



    The first sub is an object, Sheet1.
    The second is a module.
    I thought Public ment it could be used by other subs.


    I am a very base beginer. Past threads were very helpful on other issues, but this is so smiple I did not find a solution. Bthayer

  • Re: Passing Variables Between Subs


    Welcome to the forum!


    You need to add the arguments to the sub declaration, and then pass the parameters in the call:


    Code
    1. Public Sub RetriveOrder_Click()
    2. Dim OldOrderNo As Single
    3. OldOrderNo = InputBox("Retrive Order #", "Enter")
    4. ReStoreOrder OldOrderNo
    5. End Sub
    6. Sub ReStoreOrder(OldOrderNo As Single)
    7. ...

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Passing Variables Between Subs


    SHG Thank you so much. That worked great. Help me to understand the statment for future.

    Code
    1. Call ReStoreOrder(OldOrderNo)
    2. Sub ReStoreOrder(OldOrderNo As Single)


    Anytime I pass a variable between subs I must re dim it in the ( ) and also name it in the call of the sub in (). Is there no way to make it available to the other sub at the same time, or do this just once? Thank you Bthayer

  • Re: Passing Variables Between Subs


    You're quite welcome.


    The sub declaration needs to know the type of variable in order to operate with it. If all it gets as a memory address, it has no way to know if it's an integer, string, double, ... all of which store data in different ways.


    VBA then enforces that the calling routine passes compatible aruments, which is handy.


    Yes, variables can be Public. Their declarations must appear in a code module before any subs or functions.

    Code
    1. Option Explicit ' always use this; it forces you to declare all variables
    2. Public lOrderNum As Long
    3. Public aiBuf(1 to 10) as Integer
    4. Sub mYSub()
    5. ...


    See Help on the following topics:


    'understanding scope and visibility'
    'understanding the lifetime of variables'
    'declaring variables'

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Passing Variables Between Subs


    shg - this helped me tremendously! Thank you.


    To shed a bit more light on this for other users, in my testing I realized that the name of the variable that you dim as an argument in the beginning of the sub needs not be the same name of the the variable being used to pass the item in memory.


    For example, I set up a simple User Form w/ a text box and button. When the button is clicked it will store the text input into a string variable called "UserInput" and pass it when the sub test is called.

    Code
    1. Private Sub CommandButton1_Click()
    2. Dim UserInput As String
    3. UserInput = frmGenMsg.TextBox1.Value
    4. Call Test(UserInput)
    5. End Sub


    Then when the sub Test is called it takes in the string variable and dims it to a variable called "txtInput" for the "Test" sub code (which only pops up a message box w/ whatever the user typed into the user form). So not the same name is necessary. Hope that helps anyone out.


    Code
    1. Sub Test(txtInput As String)
    2. MsgBox txtInput
    3. End Sub