+ Reply to Thread
Results 1 to 3 of 3

Variable losing its value when calling new sub

  1. #1
    Registered User
    Join Date
    10-04-2005
    Posts
    11

    Question Variable losing its value when calling new sub

    Is there any way to keep a value in a variable when I call a new sub?

    I have a the same variable name in the new sub, and I want to carry over the value that I had in the first sub which is where I set the value of the variable.

    I am sure there is an easy answer to this, but I am just trying to learn as I go here..... I may be in over my head....

  2. #2
    Tom Ogilvy
    Guest

    Re: Variable losing its value when calling new sub

    If the variable is declared in each sub, then these are two separate
    variables only visible to their respective subs.

    You can declare the variable one time as public at the top of the module and
    use it in both subs (remove any other declarations)

    Public MyVar as Variant

    Sub Init_Myvar()
    MyVar = int(rnd()*1000+1)
    ShowMyVar
    End Sub

    sub ShowMyVar()
    msgbox MyVar
    End Sub

    ---------------------
    or you can past the variable in between
    --------------------
    Sub Init_Myvar()
    Dim MyVar as Variant
    MyVar = int(rnd()*1000+1)
    ShowMyVar MyVar
    End Sub

    Sub ShowMyVar(someVar as Variant)
    msgbox someVar
    End Sub

    somVar is a place holder for the variable MyVar


    --
    Regards,
    Tom Ogilvy

    "jim37055" <jim37055
    [email protected]> wrote in message
    news:[email protected]...
    >
    > Is there any way to keep a value in a variable when I call a new sub?
    >
    > I have a the same variable name in the new sub, and I want to carry
    > over the value that I had in the first sub which is where I set the
    > value of the variable.
    >
    > I am sure there is an easy answer to this, but I am just trying to
    > learn as I go here..... I may be in over my head....
    >
    >
    > --
    > jim37055
    > ------------------------------------------------------------------------
    > jim37055's Profile:

    http://www.excelforum.com/member.php...o&userid=27788
    > View this thread: http://www.excelforum.com/showthread...hreadid=473844
    >




  3. #3
    Jim Thomlinson
    Guest

    RE: Variable losing its value when calling new sub

    You can pass variables as arguments...

    Sub Test()
    dim i as integer

    i = 100
    call DisplayI(i)
    i = i + 50
    call DisplayI(i)
    end sub

    sub DisplayI(byval i as Integer)
    msgbox "The value of i is " & i
    end sub
    --
    HTH...

    Jim Thomlinson


    "jim37055" wrote:

    >
    > Is there any way to keep a value in a variable when I call a new sub?
    >
    > I have a the same variable name in the new sub, and I want to carry
    > over the value that I had in the first sub which is where I set the
    > value of the variable.
    >
    > I am sure there is an easy answer to this, but I am just trying to
    > learn as I go here..... I may be in over my head....
    >
    >
    > --
    > jim37055
    > ------------------------------------------------------------------------
    > jim37055's Profile: http://www.excelforum.com/member.php...o&userid=27788
    > View this thread: http://www.excelforum.com/showthread...hreadid=473844
    >
    >


+ 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