Excel VBA: The Little-known secrets of ByVal and ByRef

Excel VBA: The Little-known secrets of ByVal and ByRef

Excel Macro Mastery

4 года назад

45,266 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Excel Macro Mastery
Excel Macro Mastery - 20.02.2020 04:53

Enjoy the video.

Remember that it's always better to use ByVal where possible as it prevents unintended changes.

Ответить
jerkorulez
jerkorulez - 06.07.2023 22:54

Thanks Paul. This helps me a lot!

Ответить
Alter Channel
Alter Channel - 21.05.2023 11:38

Fantastic. But i have problems understanding the connection when passing the variable to one sub to another. In the first example you are passing the variable "total" and the new sub is declearing "a" as long. How does it know that "a" should be total? What is i have other long variable in my new sub "Calc" ?

Ответить
Christian Boekhoff
Christian Boekhoff - 23.11.2022 01:32

Thanks, helped me building dictionary's with other dictionary's as value. If I call a function and pass a dictionary, i do it with ByVal now to create multiple dictionary's depending on each other values.

Ответить
Kuul Dott
Kuul Dott - 09.11.2022 21:16

my god Thank you I finally was able to get to the bottom of the confusing pair of concept!! 😂 And I meant more comprehensively, in different contexts!!

Ответить
Suman Raj Manandhar
Suman Raj Manandhar - 31.10.2022 20:40

Does byVal and byRef make any difference if we use a function instead of sub?

Ответить
j mathew
j mathew - 14.05.2022 19:23

Hi. Thanks for this tutorial. I have a question to ask. Is it valid to pass objects e.g. Pivot Table, Range or Worksheet in an argument? I hope that you don't mind my asking. Would appreciate any insights.

Ответить
mark cuello
mark cuello - 11.05.2022 08:20

Help me

Ответить
Alek
Alek - 06.03.2022 01:52

Setting a new object loses all the content of the original. If ByVal worked on the object itself rather than just the pointer, then it’d be easy to copy the object as in other modern languages. Just another reason VBA is the worst programming language. It just lacks consistency in so many ways.

Ответить
Faan Müller
Faan Müller - 04.12.2021 16:05

Mr. Kelly's contributions to making Excel/VBA popular and understandable are unique and will never be equalled. Bless you, Sir!

Ответить
Gougligou
Gougligou - 21.11.2021 14:16

One point not mentioned, but worth addressing, is what happens when a sub has more than one parameter. If you ever redo this lesson, you might consider adding this point to it.
Sub proc (byval a, b, c) ——> when proc() returns, a won't have changed, but b and c could be modified since they get passed byRef.

If the programmer does not want b and c to change, then proc() has to be defined as
Sub proc (byval a, byval b, byval c)

Ответить
Marco Heerdink
Marco Heerdink - 22.07.2021 09:57

Just checked it out, i know it are the parenthesis that makes the function parameters needed to be set as a ByRef. At least, that is how it works on my job.
When i do it at home, it is the other way around

At home:
Calc total -> gives 100 at the end
Calc (total) -> gives 1 at the end


At Work:
Calc total -> gives 1 at the end

Calc (total) -> gives 100 at the end

At home i use office 2013 and at work i use offce 365
Very annoying that MicroSoft changed that between versions.

It is good to check which one you need and stick to it.

Ответить
averagebodybuilder
averagebodybuilder - 18.07.2021 21:52

ByRef is faster in terms of execution. Is that correct?

Ответить
micomc
micomc - 06.07.2021 08:16

Best channel

Ответить
Houston Vanhoy
Houston Vanhoy - 12.06.2021 01:08

Sir Paul, do you have a video dedicated to VBA keyboard shortcuts? Or would you create a new video for that?

Ответить
Shubham Bangad
Shubham Bangad - 12.03.2021 00:27

Very informative and guided with simple examples. Thanks for clearing this out.

Ответить
ChanelKh_sat
ChanelKh_sat - 09.02.2021 22:57

Thanks you are very very master

Ответить
Neetshil
Neetshil - 26.01.2021 23:32

After so long got some valued explanation for interview purpose and for real life project too. Thank you !!

Ответить
Daniel Ferry
Daniel Ferry - 28.12.2020 10:06

User Defined Types are like arrays in that they cannot be passed ByVal. UDTs can only be passed ByRef.

Ответить
Ian KR
Ian KR - 19.10.2020 12:29

This is great, but I'm trying to see the point in passing something to another sub but NOT to change its value. I must be missing something fundamental. Perhaps I need to see a real world example of both types.

Ответить
Leonardo Russo
Leonardo Russo - 27.08.2020 17:18

Awesome!!!

Ответить
jou yungcheng
jou yungcheng - 28.06.2020 07:50

really nice video,by far the most understandable explanation of difference between byval and by ref I’ve ever seen

Ответить
Michael Maguire
Michael Maguire - 24.06.2020 12:15

Very well explained. I've been using VBA on and off for >20 years and didn't know the Shift+F9 trick! Cool

Ответить
Chomik59
Chomik59 - 27.02.2020 02:06

So basically there is no point in passing collection if we are going to make new one anyway, right?
And how does it work that we can pass array ByVal as a Variant? Or should I ask why cannot we pass normal array ByVal?

Ответить
Валерий Богданов
Валерий Богданов - 21.02.2020 15:31

Привет из России. Большое спасибо за видео. Всё очень хорошо объяснено и разделено по темам. Так держать!

Ответить
Brian Burnside
Brian Burnside - 20.02.2020 04:14

I translated a python program into vba yesterday and it wasn't working correctly. I fretted over it for hours. And for some reason it dawned on me that it was a byval byref issue. Unfortunately I didn't find this video until after I resolved the issue. But great explanation.

Ответить
Cristian Croitoru
Cristian Croitoru - 20.02.2020 03:00

Can you actually change the behavior and have ByVal as the Default?

Ответить
Tughan Ozsezer
Tughan Ozsezer - 20.02.2020 02:17

I am confused about the final condition. No data has passed to the second prosedure on your example. 🙃

Ответить
Blauerbaer 87
Blauerbaer 87 - 19.02.2020 23:28

Hello your videos and the homepage helped me a lot. Keep it up bro
Greetings from germany

Ответить
Moayyad Alkeddeh
Moayyad Alkeddeh - 19.02.2020 22:43

❤️❤️

Ответить
Celia Alves - Solve & Excel
Celia Alves - Solve & Excel - 19.02.2020 19:00

Excellent lesson, Paul! I was not aware of some of the details related to passing arrays and connections. Thank you.

Ответить
João Custódio
João Custódio - 19.02.2020 18:38

Another gem of knowledge.
Thanks very much Paul.

Ответить
Archibald Tuttle
Archibald Tuttle - 19.02.2020 16:52

Nice! ByRef can lead to very dirty code when subs are used to change data. Better use functions that pass your result back.

Ответить
Julio Garcia
Julio Garcia - 19.02.2020 15:53

Very useful summary. Thank you.

Ответить
Victor
Victor - 19.02.2020 11:12

Thanks for the excellent video Paul ! Thumbs Up !!

Ответить
Wayne Edmondson
Wayne Edmondson - 19.02.2020 09:51

Hi Paul.. thanks for the video and good new information (for me anyway). Also.. I like that SHIFT+F9 keyboard for Quick Watch and then Add for the Watch window.. didn't know about that sequence before viewing this video. Always something new and interesting at Excel Macro Mastery. Thanks for all the great tips that you generously share week after week! Thumbs up!!

Ответить
RRR program
RRR program - 19.02.2020 09:35

But one more thing... Can u please elaborate... When do we use this in the real world scenario

Ответить
RRR program
RRR program - 19.02.2020 09:32

Seriously... U are VBA mann.... Simply FANTASSTICCCC.... Absolutely loved it...thanks for sharing ur exceptional knowledge paull...

Ответить
Ismail Inci
Ismail Inci - 19.02.2020 09:22

Thank you for your very clear explanation.

Ответить
Michał Gwiazdonik
Michał Gwiazdonik - 19.02.2020 09:17

Thank you Paul for this explanation :) Once again high level ;)

Ответить
Panayiotis Yannopoulos
Panayiotis Yannopoulos - 19.02.2020 08:13

I would like to thank you for your amazing good videos. You have it to be a teacher, shows a person that has searched enough to be able to pass knowledge in an easy way

Ответить
Sandeep Kothari
Sandeep Kothari - 19.02.2020 06:32

At last, I get to know the difference between the 2 terms. Thanks a lot Paul.

Ответить
Brett Nelson
Brett Nelson - 19.02.2020 06:23

Keep up these awesome VBA tutorials!!! I never know there was so many functions of ByRef! And thanks for the tip on how to look into an array with Watch :)

Ответить