+ Reply to Thread
Results 1 to 6 of 6

Learning VBA trying to understand passing paramenters and variables

  1. #1
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Learning VBA trying to understand passing paramenters and variables

    Hi group, I'm working my way through an Excel Programming book teaching VBA and I've run across an example of passing parameters to a sub routine that calls another sub routine. What is confusing me is that the parameters that are being passed (Val1 and Val2) are different than the variables used in the sub routine which calls the previous sub routine. See below:

    Sub AddValues (Val1 As Integer, Val2 As Integer)
    Dim Total As Integer
    Total = Val1 + Val2
    MsgBox (Total)
    End Sub

    Sub TotalValues
    Dim Value1 as Integer
    Dim Value2 as Integer
    Value1 = 5
    Value2 = 7
    Call AddValues (Value1, Value2)
    End Sub

    Shouldn't the first Sub AddValues actually be a Function? does the variable Val1 and Val2 become Value1 and Value2? or should the variables used be the same, ie. Val1 in the first routine stay as Val1 in the second sub routine and Val2 the same?


    Sebastien.
    Last edited by sgrondines; 11-09-2017 at 11:03 PM.

  2. #2
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: Learning VBA trying to understand passing paramenters and variables

    Quote Originally Posted by sgrondines View Post
    does the variable Val1 and Val2 become Value1 and Value2? or should the variables used be the same, ie. Val1 in the first routine stay as Val1 in the second sub routine and Val2 the same?
    I think we should talk about this first. Each subroutine processes within its own scope. There's no need to name them the same or differently, they will not share information or collide. You do want to take care not to use the same name as globally scoped variables, constants, or other functions.

    Quote Originally Posted by sgrondines View Post
    Shouldn't the first Sub AddValues actually be a Function?
    It is a little easier to understand having it be a function, and I myself would write it as a function. That said, it doesn't necessarily have to be a function. Use a function when you want to get a value returned. Use a sub when you just want it to do a thing and don't care about having it return a value.

    Here's a function implementation.
    Please Login or Register  to view this content.
    Note: It's best practice to declare the return type of your function as I did in my example, otherwise it'll be declared as a variant. then it will be hard to predict what kind of data you'll get back.
    Last edited by smpita; 11-09-2017 at 11:27 PM. Reason: Note

  3. #3
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Learning VBA trying to understand passing paramenters and variables

    I'm confused still. Does Value1 become Val1 in the first sub routine? ie. Value1 = Val1 = 5 ?

    Sebastien.

  4. #4
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: Learning VBA trying to understand passing paramenters and variables

    It's not really helpful to think of it like that because it's not really true.

    When the AddValues function (or subroutine) is called, it doesn't actually pass the variable. It either passes the value stored in the variable (byVal) or it passes a pointer reference to a memory address (byRef). In this example, they are being passed byVal (because they are integer type).

    You can think of it as the VBA interpreter reading the values inside of Value1 and Value2 and swapping them in the call to make it go from AddValues(Value1, Value2) to AddValues(5, 7).
    AddValues, then takes the passed data and assigns it to its own locally scoped variables, Val1 and Val2. They are assigned positionally unless you use the name them: AddValues(Val2:=7, Val1:=5)

    Edit:

    Another way of putting it: The subroutine TotalValues, and its variables don't close and go out of scope until AddValues completes its run and you hit the End Sub line at the end. The whole time AddValues is running, TotalValue's variable Value1 and AddValue's variable Val1 both exist in memory, albeit in isolated scopes so they don't see each other. The initial value for Val1 is set by Value1 but that's where it ends. Further changes to Val1 don't do anything to Value1 therefore they are not the same thing.
    Last edited by smpita; 11-09-2017 at 11:58 PM. Reason: Another way

  5. #5
    Registered User
    Join Date
    08-12-2012
    Location
    Calgary, Alberta
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: Learning VBA trying to understand passing paramenters and variables

    Sound good. Thank you.

    Sebastien.

  6. #6
    Forum Contributor
    Join Date
    02-26-2014
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    173

    Re: Learning VBA trying to understand passing paramenters and variables

    I made a little demo to show this behavior a little.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Passing Variables within sub
    By scottc_00 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-30-2016, 11:10 AM
  2. [SOLVED] Passing variables
    By LoftySuth in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-31-2015, 03:32 PM
  3. Cannot understand the meaning of a couple of variables
    By rasmus1166 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-09-2011, 07:14 AM
  4. Averageif funtion paramenters in VBA
    By RD8234 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-01-2010, 05:47 AM
  5. Passing Variables to Sub
    By erock24 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-21-2010, 01:28 AM
  6. Passing Variables
    By Jeff in forum Excel General
    Replies: 1
    Last Post: 11-04-2005, 02:50 PM
  7. Passing variables from one sub to another
    By Yasha Avshalumov in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-19-2005, 12:05 PM

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