+ Reply to Thread
Results 1 to 5 of 5

Passing variables from one sub to another

  1. #1
    Yasha Avshalumov
    Guest

    Passing variables from one sub to another

    I am trying to pass a variable I defined in a different sub to another one so
    I dont have to recreate the code in the new sub. I tried passing as some
    book sugggest but still having problems. I am having problems with syntax I
    believe and can't seem to find my mistake. I tried passing the variable like
    this: Sub GetNotes(LastRow as Integer) and I defined the new sub the same
    way. This give me a redifined error, saying that I am redifining the
    variable, so I tried taking out "as Integer part" but this wont even let me
    compile the code. However if I take out the parameters the code complies and
    I can step through the code, but I cant do anything with the paraments in the
    heading. Please point me in the right direction, becasue I can't seem to
    figure out what I am doing wrong.

    thanks,


  2. #2
    Bob Phillips
    Guest

    Re: Passing variables from one sub to another

    This is an example. It also shows how to ensure that any changes in the
    called module are NOT passed back to the calling module

    Sub test1()
    Dim LastRow As Long

    LastRow = 17
    MsgBox "val1 in test1: " & LastRow
    test2 LastRow
    MsgBox "val2 in test1: " & LastRow
    End Sub

    Sub test2(ByVal LastRow As Long)
    LastRow = 22
    MsgBox "val in test2: " & LastRow
    End Sub


    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Yasha Avshalumov" <YashaAvshalumov@discussions.microsoft.com> wrote in
    message news:B82355D5-E43C-4D11-A8B3-AF5CB0004928@microsoft.com...
    > I am trying to pass a variable I defined in a different sub to another one

    so
    > I dont have to recreate the code in the new sub. I tried passing as some
    > book sugggest but still having problems. I am having problems with syntax

    I
    > believe and can't seem to find my mistake. I tried passing the variable

    like
    > this: Sub GetNotes(LastRow as Integer) and I defined the new sub the same
    > way. This give me a redifined error, saying that I am redifining the
    > variable, so I tried taking out "as Integer part" but this wont even let

    me
    > compile the code. However if I take out the parameters the code complies

    and
    > I can step through the code, but I cant do anything with the paraments in

    the
    > heading. Please point me in the right direction, becasue I can't seem to
    > figure out what I am doing wrong.
    >
    > thanks,
    >




  3. #3
    Vasant Nanavati
    Guest

    Re: Passing variables from one sub to another

    Please post the relevant code.

    --

    Vasant


    "Yasha Avshalumov" <YashaAvshalumov@discussions.microsoft.com> wrote in
    message news:B82355D5-E43C-4D11-A8B3-AF5CB0004928@microsoft.com...
    >I am trying to pass a variable I defined in a different sub to another one
    >so
    > I dont have to recreate the code in the new sub. I tried passing as some
    > book sugggest but still having problems. I am having problems with syntax
    > I
    > believe and can't seem to find my mistake. I tried passing the variable
    > like
    > this: Sub GetNotes(LastRow as Integer) and I defined the new sub the same
    > way. This give me a redifined error, saying that I am redifining the
    > variable, so I tried taking out "as Integer part" but this wont even let
    > me
    > compile the code. However if I take out the parameters the code complies
    > and
    > I can step through the code, but I cant do anything with the paraments in
    > the
    > heading. Please point me in the right direction, becasue I can't seem to
    > figure out what I am doing wrong.
    >
    > thanks,
    >




  4. #4
    Jim Thomlinson
    Guest

    RE: Passing variables from one sub to another

    Here is an example of passing variables. I have passe 2 integer "This" and
    "That". This is passed by value meaning that the sub that receives the
    variable only gets a copy of the variable and can not change what the passing
    procedure sent. That on the other hand is passed by reference and changes
    made to the variable in the receiving sub alter the variable for the passing
    sub.

    Sub PassVariable()
    Dim intThis As Integer
    Dim intThat As Integer

    intThis = 10
    intThat = 50

    Call ReceiveVariable(intThis, intThat)
    MsgBox intThis & " This stayed the same."
    MsgBox intThat & " That changed."

    End Sub

    Sub ReceiveVariable(ByVal This As Integer, ByRef That As Integer)
    'Dim This as Integer 'illegal statement since This is being passed in
    and
    'you are redefining it

    This = This * 2
    That = That * 2
    MsgBox This
    MsgBox That

    End Sub

    --
    HTH...

    Jim Thomlinson


    "Yasha Avshalumov" wrote:

    > I am trying to pass a variable I defined in a different sub to another one so
    > I dont have to recreate the code in the new sub. I tried passing as some
    > book sugggest but still having problems. I am having problems with syntax I
    > believe and can't seem to find my mistake. I tried passing the variable like
    > this: Sub GetNotes(LastRow as Integer) and I defined the new sub the same
    > way. This give me a redifined error, saying that I am redifining the
    > variable, so I tried taking out "as Integer part" but this wont even let me
    > compile the code. However if I take out the parameters the code complies and
    > I can step through the code, but I cant do anything with the paraments in the
    > heading. Please point me in the right direction, becasue I can't seem to
    > figure out what I am doing wrong.
    >
    > thanks,
    >


  5. #5
    Richard Buttrey
    Guest

    Re: Passing variables from one sub to another

    On Fri, 19 Aug 2005 08:09:03 -0700, "Yasha Avshalumov"
    <YashaAvshalumov@discussions.microsoft.com> wrote:

    >I am trying to pass a variable I defined in a different sub to another one so
    >I dont have to recreate the code in the new sub. I tried passing as some
    >book sugggest but still having problems. I am having problems with syntax I
    >believe and can't seem to find my mistake. I tried passing the variable like
    >this: Sub GetNotes(LastRow as Integer) and I defined the new sub the same
    >way. This give me a redifined error, saying that I am redifining the
    >variable, so I tried taking out "as Integer part" but this wont even let me
    >compile the code. However if I take out the parameters the code complies and
    >I can step through the code, but I cant do anything with the paraments in the
    >heading. Please point me in the right direction, becasue I can't seem to
    >figure out what I am doing wrong.
    >
    >thanks,


    In the sub to which you are passing your variables, define them
    between the ( ) brackets

    In the originating sub, simply state them within the brackets of the
    Call yoursub() command.

    e.g.


    Sub GetNotes()
    Call NewSub(1, "Test")
    End Sub

    Sub NewSub(MyInteger As Integer, MyString As String)
    MsgBox MyInteger & " " & MyString
    End Sub


    HTH

    __
    Richard Buttrey
    Grappenhall, Cheshire, UK
    __________________________

+ 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