Excel VBA Introduction Part 43.1 - ByRef and ByVal

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ย. 2024

ความคิดเห็น • 63

  • @cringerfringe2285
    @cringerfringe2285 2 ปีที่แล้ว +1

    If I would have watched this video this morning I wound have wasted 4 hours today pulling my hair out. Best coding teacher online.

    • @WiseOwlTutorials
      @WiseOwlTutorials  2 ปีที่แล้ว +1

      Ahh I hate when that happens! Hope the video helped you to find a solution and thanks for watching!

    • @cringerfringe2285
      @cringerfringe2285 2 ปีที่แล้ว +1

      @@WiseOwlTutorials absolutely. Huge help. Lots of confusion on utilizing functions and passing parameters between them and subs.
      You’re making the world a better place. Keep up the great work, I’m looking forward to moving on to your other programming language lessons after I get more competent in VBA.

    • @WiseOwlTutorials
      @WiseOwlTutorials  2 ปีที่แล้ว

      @@cringerfringe2285 Comments like this make it worth doing, thanks!

  • @querrythis
    @querrythis 11 หลายเดือนก่อน +1

    Learnt so much from you. So good at explaining the fundamentals....... my current level. thanks!

    • @WiseOwlTutorials
      @WiseOwlTutorials  11 หลายเดือนก่อน

      Happy to hear that you've found the videos useful! Thanks for taking the time to leave a comment and for watching!

  • @kebincui
    @kebincui ปีที่แล้ว +1

    Thank you Andrew. This video helps me to clear my long time mist in understanding the difference between byRef and byVal. Thank you for sharing your wisdom by explaining this clearly with samples❤👍🌹

  • @scotolivera8207
    @scotolivera8207 4 ปีที่แล้ว

    You are changing peoples life with those videos

  • @memelvin1
    @memelvin1 7 ปีที่แล้ว +3

    Thanks! Can't tell you how many phone interviews where I've stumbled on this difference! Your graphic helped greatly.

  • @forzaalgiers2045
    @forzaalgiers2045 8 ปีที่แล้ว +4

    Nice to see you (Hear you ) again. We miss your videos man !!!. Thanks for the video, it's Great

    • @TonyDiaz.
      @TonyDiaz. 8 ปีที่แล้ว +1

      WiseOwlTutorials that's going to be a must but for me man.
      I've learned a lot from your vids and have made some sick apps in Excel. Thanks!!

  • @salvatorescuderi6928
    @salvatorescuderi6928 7 ปีที่แล้ว

    Hi A. Your explanation about memory (and is excel modellization) is very direct and easy. Now i understood how values and their storage in memory are treated.
    Thank

  • @zulfikarvirani
    @zulfikarvirani 8 ปีที่แล้ว +1

    I really did missed your videos sir, you are the best and i really enjoy them. I can't believe I've learnt so much and actually have applied it. Thank God you came back with more video lessons. Thank you so much sir. Please keep on making these videos.

    • @HuTrzy
      @HuTrzy 2 ปีที่แล้ว +1

      All true + 1 !

  • @Ahmed-e4w1e
    @Ahmed-e4w1e 9 หลายเดือนก่อน +1

    Excellent explanation! Thanks a lot!

    • @WiseOwlTutorials
      @WiseOwlTutorials  9 หลายเดือนก่อน

      You're very welcome, thanks for watching!

  • @donbogdala5428
    @donbogdala5428 8 ปีที่แล้ว

    Its been a while, but glad that you returned with another lesson! I really look forward to your videos and check this page on a daily basis, They are the best! I wouldn't be at the level that I am without Wiseowl Tutorials! You're the best Andrew!

    • @krn14242
      @krn14242 8 ปีที่แล้ว

      Andrew, make sure you let us know when and where the book is available. I would love to purchase a copy. If it is anything like what you have presented on TH-cam, we all could learn much more about Excel VBA.

  • @michellguzelgul4267
    @michellguzelgul4267 8 ปีที่แล้ว +3

    Your your videos are the best by far, I have been binge watching. I would Love a video on the SET key word!!!!!! So confusing.

  • @rajusinghtezu
    @rajusinghtezu 5 ปีที่แล้ว +1

    Thank you!!!..now i understood the concept behind the byval and byref..

    • @WiseOwlTutorials
      @WiseOwlTutorials  5 ปีที่แล้ว +1

      You're welcome, Raju! Happy to hear that you found the video useful and thank you for watching!

  • @MagnusAnand
    @MagnusAnand 7 ปีที่แล้ว +4

    As usual: great tutorial!

  • @marcinzajac389
    @marcinzajac389 ปีที่แล้ว +1

    Thank you. great explanation!!!

  • @oysteinoren
    @oysteinoren 8 ปีที่แล้ว

    So glad you made another one, you are definitely the best!

    • @oysteinoren
      @oysteinoren 8 ปีที่แล้ว

      Sounds fantastic, really looking forward to buy your VBA book!

    • @mjbvlc
      @mjbvlc 8 ปีที่แล้ว

      I'm sure your book will be great! Is there a mailing list I can join to be advised when it's released?

  • @ezazhaider3466
    @ezazhaider3466 5 ปีที่แล้ว +1

    Good example.

  • @jaca47
    @jaca47 7 ปีที่แล้ว +1

    You're DA REAL MVP !

  • @ouzytheoriginal
    @ouzytheoriginal 2 ปีที่แล้ว +1

    thanks clarified some points

    • @WiseOwlTutorials
      @WiseOwlTutorials  2 ปีที่แล้ว

      Happy to hear that it helped, thanks for watching!

  • @MouradBENKADOUR
    @MouradBENKADOUR 6 ปีที่แล้ว

    Nice Tutorial! But I didn't really understand the error in 23:08 minutes, because if you declare an Integer Array (Dim Mins() as Integer) it will work well. But you can use ByRef as Integer to change an Array as Variant. But in other Hand you have to use a Variant Array to store String after Integer. Thanks!

  • @laurenceabrahams3996
    @laurenceabrahams3996 8 ปีที่แล้ว

    Finally understood this , many thanks

  • @henrikijonkoping4694
    @henrikijonkoping4694 10 หลายเดือนก่อน

    Great stream

  • @muhammedcansoy6131
    @muhammedcansoy6131 3 ปีที่แล้ว +1

    excellent

  • @easyspeak101
    @easyspeak101 ปีที่แล้ว

    excellent lesson.. How does it relate to temporary, private or public variables passing between modules or the fact that a pair of temp variables rather than byref or byval..? Thx tho..😁

  • @jmathew6988
    @jmathew6988 2 ปีที่แล้ว

    Thank you! 🙏
    "The Force Awakens" ought to be in 4th position. 😊
    Sir, I have two question if you could kindly answer.
    (1) Can an Argument and Parameter variable inside Caller and Called subroutines respectively be identically named, or is there any downside to this practice?
    (2) Given that a called procedure only needs to refer a parameter (without changing it's value), but there also is a need to minimize memory usage. Is it ideal to choose ByRef over ByVal?
    I hope that you do not mind my asking. Would appreciate any insights.

  • @thomasfergusen5144
    @thomasfergusen5144 4 ปีที่แล้ว

    really good knowledge, thanks!

  • @petravalcheva6766
    @petravalcheva6766 7 ปีที่แล้ว +1

    The best !

  • @alvaradooutdoor
    @alvaradooutdoor 5 ปีที่แล้ว

    I love your videos

  • @bunc11
    @bunc11 5 ปีที่แล้ว

    interesting...if in SetANumber sub you change First line intoconst OriginalNum as integer = 5at the end the value stays the same, of course!Even if in IncreaseANumber sub you explicitly write byref... no compile errors :)

  • @ashrafkader1972
    @ashrafkader1972 2 ปีที่แล้ว +1

    Great

  • @rustamtau7944
    @rustamtau7944 3 ปีที่แล้ว

    Great Thanks for the video. But i still have a question regarding the task i am tackling now.
    How to pass the OptionButton value to the main Sub?
    I have a macro that picks some rows in a table based on criterium. Then i want to allow the user select where he wants to place the result. So i made simple UserForm with 2 OptionButtons. When the user clicks "ok", i somehow need to pass my variable "i" that holds the value of selected OptionButton to the main Sub (if OptionButton1=1 Then i=1, if OptionButton2=1 Then i=2)
    But how to do it?

    • @WiseOwlTutorials
      @WiseOwlTutorials  3 ปีที่แล้ว

      Hi! There are some good answers here stackoverflow.com/questions/51952304/return-a-value-from-a-userform
      I hope it helps!

  • @vijaysahal4556
    @vijaysahal4556 3 ปีที่แล้ว +1

    nice 👍🏻👍🏻👍🏻👍🏻👍🏻

  • @eziola
    @eziola 8 ปีที่แล้ว

    For the Mins array did you mean to type:
    Mins = Range("B2", Range("B2").End(xlDown)) ?

  • @etorebellino
    @etorebellino 6 ปีที่แล้ว

    Thanks so much

  • @leeeric6292
    @leeeric6292 3 ปีที่แล้ว

    Thanks for your video. In 23:10, u just show error occurred because data type don't match if byref is applied, however, could u explain a little more details why? And why using byval can solve the problem ? Thanks.

    • @WiseOwlTutorials
      @WiseOwlTutorials  3 ปีที่แล้ว

      Hi Lee! When you assign a range to an array, the data type of the values is Variant and the subtype is determined by the values of the cells (in this case it's Double because the cells all contain numbers). If we pass the one of the array elements into another procedure ByRef, the data type of the parameter needs to match. In this example, it doesn't - the m parameter has a data type of Integer. When passing information ByRef the data types need to match because you're pointing to the same location in memory. When passing ByVal you make a copy of the original value in a separate location in memory which can have whatever data type you like (VBA performs the implicit conversion of Variant/Double to Integer). You can solve the problem in at least two other ways:
      1) Explicitly convert the value of the array element before passing it to the ByRef parameter
      Mins(i, 1) = MinsToHours(CInt(Mins(i, 1)))
      2) Change the data type of the ByRef parameter to Variant
      Function MinsToHours(m As Variant) As String
      I hope that helps!

    • @leeeric6292
      @leeeric6292 3 ปีที่แล้ว +1

      @@WiseOwlTutorials Many Thanks for ur fast response and help. I think i need some time to digest and practice what you just have mentioned. Again, Thanks !

    • @WiseOwlTutorials
      @WiseOwlTutorials  3 ปีที่แล้ว

      @@leeeric6292 My pleasure Lee! And yes, ByRef and ByVal aren't intuitive but practice certainly helps. good luck!

    • @leeeric6292
      @leeeric6292 3 ปีที่แล้ว

      @@WiseOwlTutorials Thanks for your two solutions last time: mins(i,1)=minstohours(CINT(mins(i,1)))and function minstohours(m as variant) as string. I have tried both of them and of course they work. Apart from them, I also try below :
      In your last reply, you said in the example of the video using Byref don't work because the values of cell is in Double datatype while the parameter "m" in the function is defined as integer, they need same data type because in Byref they share the same memory location, so I also try this : function minstohours(m as double) as string. However datatype mismatch error pop up when I run the program, is there something I get wrong?
      Secondly, in your last reply, u said when passing Byval, VBA performs the implicit conversion of Variant/Double to integer. So I believe there is a change in data type operation in Byval, that is the Data type from the call procedure change to the Data type of the parameter in that function when Byval is applied. If this assumption is true, what happens if the Data type from the call procedure is in string in this example, what then the result in the ByVal function would be, I also have tried, again errors window is also popped up. So did it imply that the Byval performs the conversion of Variant/Double to integer only?
      Thanks.

    • @WiseOwlTutorials
      @WiseOwlTutorials  3 ปีที่แล้ว

      @@leeeric6292 Hi Lee! The data type of the value in the array is Variant with a subtype of Double - not quite the same thing as a Double. The subtype of Double is assigned at run time when we assign the range to the array. When the project is compiled (when the error message appears), the function doesn't know what subtype the variant will have - it only knows that the value being passed to the Function will be a Variant while the function expects an Integer - a Variant and an Integer can't both occupy the same space in memory at the same time, hence the error.
      Secondly, yes, when passing ByVal, the data type of the copy of the value is coerced into the data type of the parameter if it is possible to do so. You can't convert the String "elephant" into an Integer so this would cause a Runtime Error when the code reaches the value which can't be converted. Note that this is different to the Compile Error that occurs when the data type of the ByRef parameter doesn't match the type of the value being passed to it.
      I hope that helps!

  • @bunc11
    @bunc11 8 ปีที่แล้ว

    Sub test()
    Dim temp As Integer
    temp = 35
    inc temp
    Debug.Print "Temp is now: " & temp
    End Sub
    Sub inc(num As Integer)
    num = num + 1
    Debug.Print "Num is now: " & num
    End Sub
    Result:
    Num is now: 36
    Temp is now: 36
    change inc temp to inc (temp) aka just add parenthesis
    Result:
    Num is now: 36
    Temp is now: 35
    WHAT IS GOINNG ON??

    • @bunc11
      @bunc11 8 ปีที่แล้ว

      Thx :)

    • @bunc11
      @bunc11 8 ปีที่แล้ว

      yes i am :D thx :)

  • @chalardasdamongkol8618
    @chalardasdamongkol8618 5 ปีที่แล้ว

    Speak too fast