Trying to understand ByRef and ByVal

spencer_time

Board Regular
Joined
Sep 19, 2019
Messages
55
Office Version
  1. 365
  2. 2016
  3. 2007
Platform
  1. Windows
Hello again guys, I'm still trying to learn VBA and excel macros as best and fast as I can and one of the ways I have been doing that is posting my problems for you guys to help me with AND looking at others posts and attempting to help them with their problems.

I was trying to help someone and noticed the flag ByVal in front of a variable in a function and didn't know what it was.

Some quick google searching revealed that ByRef is the default and excel uses the argument as a reference, ByVal can be used instead to force excel to use the argument as a value instead.

I have experimented with both of them trying to understand the difference in a practical sense and I can't notice anything change.....

What I am asking is what is the difference between ByRef and ByVal and when would you want to use ByVal?

Thanks in advance for your time and help.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Its easier to understand ByRef and ByVal with values rather than objects.

The variables passed to a routine ByRef are effected by changes that routine makes. ByVal values do not cause changes in the calling arguments

Consider

Code:
Sub Test()
    Dim A as Long, B as Long

    A = 1
    B = Incriment(A)

    MsgBox A & vbcr & B
End Sub

Function Incriment(ByVal X as Long) As Long
    X = X + 1
    Incriment = X
End Function
That passes A to Incirment ByVal and adding 1 to the X argument does nothing to A.

But in this situation
Code:
Sub Test2()
    Dim A as Long, B as Long

    A = 1
    B = Incriment(A)

    MsgBox A & vbcr & B
End Sub

Function Incriment2(ByRef X as Long) As Long
    X = X + 1
    Incriment2 = X
End Function
The argument is passed ByRef and the X=X+1 effects both X and A.
 
Last edited:
Upvote 0
Solution
Thank you mikerickson, that (although I suspect it had a typo, calling the same function from both test and test2 sub routines) cleared it up easier than a whole night of googleing could have done, I now understand that ByVal is used when you don't want your variable to change.

EDIT 22:43CST: fixed typo
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top