+ Reply to Thread
Results 1 to 22 of 22

Passing Arguments To Procedures

  1. #1
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Passing Arguments To Procedures

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    1) Is MyValue = YourValue here (10)? (Because it's argument of the same method.)

    2) If so Why they are written the same as "MyValue"?

    3) Sometimes there is not a argument next to ByVal, ByRef. Why?
    Last edited by zanshin777; 01-13-2016 at 10:40 AM.

  2. #2
    Registered User
    Join Date
    02-09-2014
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    60

    Re: Passing Arguments To Procedures

    1. MyValue will still be 10 as you have passed the argument as ByVal.
    2. By default the argument is passed as ByRef so when you do not mention anything it is ByRef.

    Hope this answers your query.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Passing Arguments To Procedures

    1 Not sure what you mean, the value of MyValue is passed to Process so within Process YourValue has the value of MyValue.

    2 If you leave out ByVal/ByRef the argument is passed ByRef by default.
    If posting code please use code tags, see here.

  4. #4
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    I mean on 1st and 2nd question;

    What's the difference with that macro and this macro;

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures


  6. #6
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: Passing Arguments To Procedures

    Hello Zanshin! have a look at
    https://msdn.microsoft.com/en-us/lib...=vs.60%29.aspx

    this may be helpful to your query
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    WANT TO SAY THANKS, HIT ADD REPUTATION (*) AT THE BOTTOM LEFT CORNER OF THE POST

    More we learn about excel, more it shows us, how less we know about it.

    for chemistry
    https://www.youtube.com/c/chemistrybyshivaansh

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,386

    Re: Passing Arguments To Procedures

    If I may, a lot of your questions on the forum suggest that you are taking a course in programming. If this is true, I wonder if explaining the broader context (what unit in the course you are currently studying, what you are supposed to be learning, etc.) might help us better understand your questions, and give more helpful answers.

    Thoughts on these specific questions:

    1) If myvalue=yourvalue here? -- Where is "here" in the context of this question? The "obvious" (to me anyway) answer to this question is to execute the code one step at a time (I assume you have learned this as one of your debugging strategies) while monitoring the variables in the "Locals" window (another debugging tool that I assume you are familiar with) and/or the "Watch" window. Stepping through the code in my head, it seems pretty obvious to me that, after entering "Process" and before executing that statement, myvalue should indeed equal yourvalue. However, as soon as that statement is executed, they will no longer be equal.

    2) I do no understand this question. Perhaps explain the context for this question or something to help understand it better.

    3) Sometimes there is not an argument next to byval? -- Again, I don't think I understand the question. If you have ByVal without an argument, you should get a syntax error. As the others have noted, if you leave out ByVal, then ByRef is assumed by VBA. The difference between ByVal and ByRef is explained in hemesh's link.

    Finally, the difference between the code in the OP and the code in post #4? The only difference I can see is the name of the argument in the Process procedure. Otherwise, the two sets of procedures appear to do the same things with almost no difference.

    Some questions to help think through what I think you are supposed to be learning here.
    What is your understanding of the difference between passing an argument ByRef and ByVal?
    What is your understanding of the scope of a variable?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    My questions #1 and #2 originate from here;


    Maybe this statement should have been like this;

    YourValue = MyValue*10

    Rather than

    YourValue = YourValue*10

    Because;

    1) Mathematically this equilibrium "YourValue = YourValue*10" is wrong unless "YourValue" is 0

    2) "YourValue = MyValue*10" Since MyValue = 10, "YourValue" gets 100 as it should be.
    Last edited by zanshin777; 01-17-2016 at 08:29 PM.

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,386

    Re: Passing Arguments To Procedures

    Remember that these are computer assignment statements ("Let statements" if you will, though the keyword "Let" is often or nearly always optional and not included.) These are not true "mathematical" equalities like you are seeing in question 1. Think of an assignment statement like as saying "Let the value of the storage location named on the left of the equal sign be assigned the value of the expression on the right of the equal sign." I can't recall what language it was (Pascal maybe) that actually used a different symbol := for these kind of things that, as I was taught, did not really mean "equal" but "becomes". A statement like Yourvalue=yourvalue*10 would be read more like "The new value of 'yourvalue' becomes the old value of 'yourvalue' multiplied by 10".

    The main problem with observation 2) applied to the first pair of procedures is that MyValue's scope is limited to Procedure Main. If you have a statement like
    yourvalue=myvalue*10
    in Process is that myvalue has not meaning outside of its scope. If you don't have option Explicit turned on, then myvalue in Process has the value of 0 and not 10 as assigned in Main. If your course work has not yet covered the topic of scope, then you can expect to learn this aspect of variables (and I would expect to learn it soon). If it has covered this topic, I would suggest you go back and review what you have learned about scope.

  10. #10
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    How to use;

    1) Immediate Window?

    2) Locals Window?

    3) Watch Window?

    (in VBA)

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,386

    Re: Passing Arguments To Procedures

    http://www.cpearson.com/Excel/DebuggingVBA.aspx or put something like "VBA debugging tools" into your favorite internet search engine.

  12. #12
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    Thank you very much.

  13. #13
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    What would be the result if there weren't "ByVal".

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Passing Arguments To Procedures

    Try it and see?

  15. #15
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    Without ByVal the result is 10. Why?

  16. #16
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,386

    Re: Passing Arguments To Procedures

    Since I don't see a variable named "result", what do you mean when you say that the result is 10? Where in the procedure(s) are you observing this result (beginning, during, after completed)? Are you talking about the code variation in post #1, or the one in post #4? Did you expect the result to be 10, or were you expecting a different result? If you were expecting a different result and stepped through the code, at what point in the code did you notice that the result is different from what you expected?

  17. #17
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    I mean the process is calculated then MsgBox gives message "10" as result.

  18. #18
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,386

    Re: Passing Arguments To Procedures

    Is that what you expected to see, or did you expect to see 100? Did you step through the code, and what did you learn from that process about what is happening?

    Based on the code exactly as it is written, 10 is the result I would expect to see in that message box. However, you indicate that you also get 10 when you delete ByVal from Process. This is different from what I get. When I delete ByVal from Process, I get 100 in the message box. You might double check your results.

    The difference is in passing arguments byval and byref. Did you read through the link in post#6 and did you understand what it said about passing arguments byval and byref? If not, you might try this help file that goes into a little more detail: https://msdn.microsoft.com/en-us/library/ddck1z30.aspx
    or http://www.techrepublic.com/article/...yref-keywords/
    or https://social.msdn.microsoft.com/fo...yval-and-byref
    You might also put "difference between byref and byval visual basic" into your favorite internet search engine and see what other results you find.

    In addition to the examples given in these links, what happens from this set of procedures, where one procedure receives the argument byref and the other receives it byval?
    Please Login or Register  to view this content.
    When all of this is said and done, what is your understanding of the difference between "byref" and "byval"?

  19. #19
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    Sorry, it's fault of mine.

    The results are exactly what you said.


    Case1: With ByVal the result returns "10"

    As far as I understand it should have gone in this way;

    It should have been multiplied 10 with 10. New YourValue = YourValue * 10

    MyValue = 10

    YourValue = MyValue = 10

    New YourValue = 10 * 10 => 100

    Case2: If I delete ByVal the result returns "100"

    I've read the text you've mentioned.

    Can you explain why the result is 10 and 100 in those cases

  20. #20
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,386

    Re: Passing Arguments To Procedures

    It appears to me that you need to read these more carefully, because you don't appear to correctly understand how ByVal works. I'm not sure if I can explain it any better than what has been linked to.

    Case1: With ByVal the result returns "10"
    As far as I understand it should have gone in this way;
    It should have been multiplied 10 with 10. New YourValue = YourValue * 10
    MyValue = 10
    Up to this point, your expectation is correct. MyValue contains 10 and YourValue contains 100 (10*10). However, this next expectation is consistent with ByRef, but not ByVal.
    YourValue = MyValue = 10
    New YourValue = 10 * 10 => 100
    Reading between the lines a little, it appears that you expect YourValue and MyValue to always be equal. What your textbook and these links are trying to explain is that, when you pass an argument ByVal, the memory location named YourValue is not referring to the same memory location as MyValue in the calling procedure. Changes to YourValue in Process are not reflected in MyValue in the calling procedure. When an argument is passed By Ref, then the two variable names are referring to the same memory location. When passed By Ref, changes to YourValue in the called procedure are reflected in MyValue in the calling procedure.

    I recognize that, for all intents and purposes, that is really just a repeat of what the other links have been saying, so I'm not sure that I have done any better at explaining this than the other links have.

  21. #21
    Forum Contributor
    Join Date
    01-16-2015
    Location
    Istanbul
    MS-Off Ver
    Office 365
    Posts
    928

    Re: Passing Arguments To Procedures

    When there is ByVal it protects the argument from changing.

    How does it happen? Here for example;

    By cancelling the called procedure? (The procedure named Process)

    Am I right?

  22. #22
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Passing Arguments To Procedures

    No, it passes a copy of the variable, byRef passes the actual variable (though it doesn't stop the argument changing if the argument is an object)

+ 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 2 arguments
    By PeterMac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-11-2015, 05:34 AM
  2. [SOLVED] Passing Arguments to PRIVATE Procedures
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-24-2014, 10:34 AM
  3. [SOLVED] Executing procedures with multiple arguments
    By ferfer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-21-2012, 03:40 AM
  4. Passing Arguments
    By cosmarchy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-28-2010, 04:36 PM
  5. UserForm Procedures with Arguments?
    By CrazyFileMaker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-01-2009, 02:09 AM
  6. [SOLVED] Passing arguments from VBA to DLL
    By mkluwe@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-18-2006, 04:15 AM
  7. Passing Variables Between Procedures
    By Arturo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2005, 10:06 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