+ Reply to Thread
Results 1 to 13 of 13

passing object byVal or byref

  1. #1
    Registered User
    Join Date
    04-11-2016
    Location
    Leeds
    MS-Off Ver
    MS office
    Posts
    7

    passing object byVal or byref

    I have got this codes as below from the web. but I don't quite understand it. can someone explain please?

    why the output is

    AFTER CALL::Range1: A3 =
    AFTER CALL::Range2: A2 = 654







    Please Login or Register  to view this content.
    correct output :

    BEFORE CALL::Range1: A1 = 123
    BEFORE CALL::Range2: A2 = 456
    AFTER CALL::Range1: A3 =
    AFTER CALL::Range2: A2 = 654



    Kind regards

    Alex

  2. #2
    Registered User
    Join Date
    04-11-2016
    Location
    Leeds
    MS-Off Ver
    MS office
    Posts
    7

    Re: passing object byVal or byref

    Hi Everyone

    My question is mainly on a couple of lines of the code.


    CalledProcedure R1:=Range1, R2:=Range2

    does this line mean the R1 will be assigned the cell A1 and R2 with A2. so when calledProcedure is run, A1.Value=321 and A2.value=654????? I am still confused.


    Kind regards

    Alex

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: passing object byVal or byref

    Hi, and welcome to the forum.

    That looks like one of Chip Pearson's example on his web site. Does his explanation along with it not help?

    When you pass an argument to a procedure by reference it uses the original value. The variable value is changed in the function.

    When you pass an argument by value you pass a copy to the function. The original value is not changed.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    04-11-2016
    Location
    Leeds
    MS-Off Ver
    MS office
    Posts
    7

    Re: passing object byVal or byref

    Quote Originally Posted by Richard Buttrey View Post
    Hi, and welcome to the forum.

    That looks like one of Chip Pearson's example on his web site. Does his explanation along with it not help?

    When you pass an argument to a procedure by reference it uses the original value. The variable value is changed in the function.

    When you pass an argument by value you pass a copy to the function. The original value is not changed.


    Hi Richard
    it is Chip Pearson's example. As I am new to vba . Althought there is explanation. It is hard for me to understand.

    When you pass an argument to a procedure by reference it uses the original value. The variable value is changed in the function.

    When you pass an argument by value you pass a copy to the function. The original value is not changed.
    as what you explained, should the answer be

    AFTER CALL::Range1: A3 = 321 (ByRef The variable value is changed in the function.)
    AFTER CALL::Range2: A2 = 456 ( ByVal The original value is not changed.)



    Kind regards

    Alex

  5. #5
    Registered User
    Join Date
    04-11-2016
    Location
    Leeds
    MS-Off Ver
    MS office
    Posts
    7

    Re: passing object byVal or byref

    Hi Richard

    but the correct output is

    AFTER CALL::Range1: A3 =
    AFTER CALL::Range2: A2 = 654

    Very confusing for me.


    Kind regards

    Alex

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: passing object byVal or byref

    Quote Originally Posted by alexba View Post
    Hi Richard

    but the correct output is

    AFTER CALL::Range1: A3 =
    AFTER CALL::Range2: A2 = 654

    .
    Alex
    Indeed. And that's what the code gives you. The R1 By Ref argument is modified in the Called Procedure by the A3 Value which is blank. Perhaps it's easier to understand if you comment out the
    R1.Value = 321
    R2.Value = 654

    Then the output is

    BEFORE CALL::Range1: A1 = 123
    BEFORE CALL::Range2: A2 = 456
    AFTER CALL::Range1: A3 =
    AFTER CALL::Range2: A2 = 456

    i.e R1 has been modified to reflect the A3 vale, whereas although you set R2 to be the A4 value , i.e. Nil, it still carries the original value.

  7. #7
    Registered User
    Join Date
    04-11-2016
    Location
    Leeds
    MS-Off Ver
    MS office
    Posts
    7

    Re: passing object byVal or byref

    Hi Richard

    Thank for your quick reply . last question please. hopefully my explanation make sense on the right of the code.

    Please Login or Register  to view this content.
    so when the code is run. the output is
    AFTER CALL::Range1: A3 = because passing object by ref, the object is changed from A1 to A3, but as cell A3 is blank.
    AFTER CALL::Range2: A2 = 456 because passing object by val, the object is not changed ,stil A2, but as cell A2 is assigned a new value 654 .


    Kind regards

    Alex

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: passing object byVal or byref


  9. #9
    Registered User
    Join Date
    04-11-2016
    Location
    Leeds
    MS-Off Ver
    MS office
    Posts
    7

    Re: passing object byVal or byref

    Hi,

    Sorry, I am new to vba. I just wanna be clear about this completely.


    Kind regards

    Alex

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,907

    Re: passing object byVal or byref

    Then maybe you should respond to the people trying to explain it to you rather than simply starting a new conversation elsewhere.

  11. #11
    Registered User
    Join Date
    04-11-2016
    Location
    Leeds
    MS-Off Ver
    MS office
    Posts
    7

    Re: passing object byVal or byref

    yes, you are right . thanks for your advice.

    Kind regards

    Alex

  12. #12
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: passing object byVal or byref

    Hi Alex

    I was just about to give You the same explanation given to You by Rory over there. ( Post #6 ). So he saved me some time. I can recommend that explanation
    Just one last point to be sure you caught.

    It may be worth remembering that Passing Objects are a special case of passing ByVal or ByRef.

    Most Things:

    For most things passing a Variable ByRef means changes done to that in the called routine will be reflected in ( referenced to ) the Original ( ORefiginal ) Variable. I think we all agree there.

    For most things passing a variable ByVal means at the Call a full copy is made of the original Variable. Changes done to that Copy Variable in the called routine will be reflected in ( referenced to ) that Copy variable. That Copy variable “dies” at the end of the Called routine. So the original is not affected in any way. I think we all agree there also


    For Objects:

    These are a special case.

    The ByRef case is similar to before.**

    The ByVal case is subtly different. Check out again Rory’s “Piece of paper” explanation: For ByVal for an Object you do not make a full copy as in most cases. You copy that piece of paper. So Both the original Paper and the Copy Paper references the Original Object. The “instructions” on the two pieces of paper ( concerning things like where the Values of that Object are stored and what values they have etc.... ) are the same. Reading either piece of paper causes you to do the same thing. So changes done ByVal in the called procedure will still be referenced back to the original Object. Just as they were in the ByRef case. But if you change that Object itself in the ByVal procedure, such as trying to offset it to a different cell, ( different cell = Different Object for a Range object for example ) you will see the change is affected in the Called routine, but not to the Original Object. The Copied piece of paper “dies” at the end of the called procedure. The Object you used in that called procedure dies with it. I am not clear where that Copy Object comes from. Apparantly, according to our explanations, it was not copied from the original. Maybe it is just a local Variable of the type of the original, but with the difference that it does not get it's own unique bit of paper - it gets that copied paper

    Alan

    P.s. Note I said similar **. At the Call you hand over the original piece of paper ByRef
    So for Objects you allways pass a Piece of paper, the original ( ByRef ) or a copy thereof (ByCopVyalue ) . For Most other things you go to a Pigeon Hole where the paper is. That Pigeon Hole being the Original Pigeion Hole ByRef or the temporary built / copied from the original one when ByVal ( This tempory oigein Hole is demolished with all in it at the end of the calls Procedure ) .
    Not sure what that small subtle difference means between the Paper and the Pigeion Hole with a paper in it. . I doubt anyone does as it is all sounds like a right load of old bollox anyway ; )

    http://excelmatters.com/2016/03/10/b...he-difference/
    http://www.mrexcel.com/forum/excel-q...val-byref.html
    http://www.mrexcel.com/forum/excel-q...l-byref-3.html

    P. P.s.
    I made some notes for myself on all this ByRef ByVal stuff
    A copy is here:

    http://www.excelforum.com/showthread...t=#post4381274
    http://www.excelforum.com/showthread...t=#post4381275
    http://www.excelforum.com/showthread...t=#post4381420
    Last edited by Doc.AElstein; 05-08-2016 at 04:15 PM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: passing object byVal or byref

    Quote Originally Posted by alexba View Post
    Hi Richard

    Thank for your quick reply . last question please. hopefully my explanation make sense on the right of the code.

    Please Login or Register  to view this content.
    so when the code is run. the output is
    AFTER CALL::Range1: A3 = because passing object by ref, the object is changed from A1 to A3, but as cell A3 is blank.
    AFTER CALL::Range2: A2 = 456 because passing object by val, the object is not changed ,stil A2, but as cell A2 is assigned a new value 654 .


    Kind regards

    Alex
    Hi,

    Essentially Yes. But don't confuse cells on the sheet with a VBA variable
    You suggest above
    AFTER CALL::Range2: A2 = 456 because passing object by val, the object is not changed ,stil A2, but as cell A2 is assigned a new value 654 .

    A2 has NOT been assigned a new value of 654. Nowhere in the code is A2 assigned (or in normal parlance has been populated with ) a value. It's the variable R2 that has been assigned the value 654 and that is NOT modified by the Set R2 = Range("A4"). R2 is a 'By Value' variable and Bt Value variables are not modified.

+ 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. Call ByVal ByRef StrPtr Address. Please Run a Code for me and post me the results.
    By Doc.AElstein in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2016, 11:58 AM
  2. [SOLVED] Workbook_SheetActivate(ByVal Sh As Object) QUESTION...
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-24-2015, 07:35 AM
  3. [SOLVED] Fix an error in MODULE to run Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-28-2015, 12:40 PM
  4. [SOLVED] Problem with Workbook_SheetCalculate(ByVal Sh As Object)
    By trimmjens in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-07-2015, 07:47 AM
  5. [SOLVED] Learning differences between ByVal and ByRef
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 07-24-2013, 07:48 PM
  6. [SOLVED] ByRef argument type mismatch / Passing array as a function argument
    By pzling in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-29-2012, 06:23 PM
  7. Error passing element of string array get ByRef argument type mismatch
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2011, 02:59 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