Consulting

Results 1 to 4 of 4

Thread: ByRef ByVal

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    ByRef ByVal

    Can one of you tell me how these are used. Im looking for what ByVal actually does as well as ByRef. I've read on the net about them but hoping one of you would be able to give a better explaination (at least one I can understand). Thanks.
    Peace of mind is found in some of the strangest places.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    By reference (ByRef) and by value (ByVal) are ways of passing values to a procedure.

    When you pass by reference, you are passing a reference to the value, or its memory location. This means that the called procedure can modify that variable and the change will be seen within the calling procedure

    [vba]

    Public Sub TestByRef()
    Dim myVar As Long

    myVar = 20
    MsgBox "myVar before call (in calling proc): " & myVar
    Call ByRefProc(myVar)
    MsgBox "myVar after call (in calling proc): " & myVar
    End Sub

    Public Sub ByRefProc(ByRef AVariable As Long)
    MsgBox "myVar at start (in called proc): " & AVariable
    AVariable = Int(Rnd() * 100)
    MsgBox "myVar after change (in called proc): " & AVariable
    End Sub
    [/vba]

    When a variable is passed by value, a copy of that variable is taken and used b y the called procedure, and cleared at the end. Thus, any changes to it in the called procedure will be lost on exit, so the calling procedure will see the same value that it passed to that called procedure.

    [vba]

    Public Sub TestByRef()
    Dim myVar As Long

    myVar = 20
    MsgBox "myVar before call (in calling proc): " & myVar
    Call ByRefProc(myVar)
    MsgBox "myVar after call (in calling proc): " & myVar
    End Sub

    Public Sub ByRefProc(ByVal AVariable As Long)
    MsgBox "myVar at start (in called proc): " & AVariable
    AVariable = Int(Rnd() * 100)
    MsgBox "myVar after change (in called proc): " & AVariable
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I believe that Objects, Strings, and Arrays are allways passed ByRef

    Chip Pearson (my other source for all things VBA when XLD's on vacation) also has a nice write up and some further examples

    http://www.cpearson.com/Excel/byrefbyval.aspx

    Paul

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks guys
    Peace of mind is found in some of the strangest places.

Posting Permissions

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