Put ByVal before a parameter in the procedure’s declaration to pass a copy of the value. The called procedure can change its copy, and the caller’s variable keeps its original value:
Sub Main()
Dim n As Long
n = 10
AddFive n
MsgBox n 'still 10
End Sub
Sub AddFive(ByVal x As Long)
x = x + 5
End Sub
Without a keyword, VBA passes arguments ByRef, which hands over the variable itself. Remove ByVal from the example above and the message shows 15, because AddFive changed n.
ByVal also avoids the “ByRef argument type mismatch” compile error. A ByRef parameter declared As Long needs the caller to pass a variable declared As Long; with ByVal, VBA converts any compatible value.
Two things catch people out. Wrapping an argument in brackets, as in AddFive (n), passes a copy even to a ByRef parameter. And ByVal on an object such as a Range copies only the reference, so the procedure can still change the cells’ values and formatting.
What Is ByVal
ByVal (by value) in VBA is a keyword you use with an argument of a procedure to pass a copy of a variable’s value to that procedure, instead of the variable itself. Let’s say a variable has a value of 20 in a procedure. Now, when you pass it ByVal to another procedure, that procedure gets the value 20, can use it as a base, and can make changes to it. But those changes don’t go back to the original variable, so its value stays 20.
ByVal is the opposite of ByRef (by reference), which passes the variable itself. In VBA, ByRef is the default. So, when you don’t write either keyword with an argument, VBA passes it ByRef.
How to Use ByVal in VBA
In the example below, we have two procedures, MACRO1 and MACRO2. In MACRO1, we have a variable named “myNumber”. The best way to understand this is to understand the code.

So, to understand how ByVal works, we need to understand this code line by line.
- First, we have assigned the value 10 to the variable. We have assigned this value in the procedure MACRO1.
- After that, we called the second procedure MACRO2 from the first procedure and passed the variable to it as an argument. This runs the second procedure from the first procedure.
- In the second procedure, we have used the ByVal keyword with the argument (myNumber) in the procedure’s declaration. This means MACRO2 gets a copy of the variable’s value, not the variable itself.
- In the second procedure, we used the variable and added a value (5) to the existing value we got from the first procedure. The variable’s original value was 10, and we added 5, meaning the current value will be 15.

- In the end, we have used the message box function to show the variable’s value. It showed 15 in the result, which is 10 + 5. 10 is the value the variable got from the first procedure, and 5 is what we have added to it in the current procedure.
'first macro Sub MACRO1() Dim myNumber As Integer myNumber = 10 MACRO2 myNumber End Sub 'second macro Sub MACRO2(ByVal myNumber As Integer) myNumber = myNumber + 5 MsgBox myNumber End Sub
Once the second (current) procedure is complete, the variable in the first procedure still has the value we assigned to it there, because MACRO2 only changed its own copy of the value.
To help you understand this, we have changed the above code slightly. We have added a message box in the first procedure (MACRO1) after calling the second procedure (MACRO2).
'first macro Sub MACRO1() Dim myNumber As Integer myNumber = 10 MACRO2 myNumber MsgBox myNumber End Sub 'second macro Sub MACRO2(ByVal myNumber As Integer) myNumber = myNumber + 5 MsgBox myNumber End Sub
Now, in this code, when you run it, it shows you two message boxes, first from MACRO2 and then from MACRO1.

In the first message box, you can see it shows 15, which is the value of the variable in the second procedure. It includes the value from the first procedure and the value added in the second procedure.
After that, in the second message box, we have the variable’s original value, assigned in the first procedure. The change made in MACRO2 didn’t reach it.
ByVal vs ByRef in VBA
Now, let’s see what happens when you use ByRef instead. In the code below, I have only changed ByVal to ByRef in MACRO2. If you try it, replace the earlier code with this one, as a module can’t have two procedures with the same name.
'first macro
Sub MACRO1()
Dim myNumber As Integer
myNumber = 10
MACRO2 myNumber
MsgBox myNumber
End Sub
'second macro
Sub MACRO2(ByRef myNumber As Integer)
myNumber = myNumber + 5
MsgBox myNumber
End Sub
This time, both message boxes show 15. With ByRef, MACRO2 works on the same variable as MACRO1, so when it adds 5, the variable in MACRO1 changes too. You get the same result if you remove ByRef and write just myNumber As Integer, because ByRef is the default.
- ByVal: The procedure gets a copy of the value. Any change it makes stays inside that procedure, and the original variable keeps its value.
- ByRef: The procedure gets the variable itself. Any change it makes goes back to the original variable. This is the default in VBA.
Use ByVal in a Function
ByVal works the same way in a Function procedure. In the code below, the function AddFive gets a copy of the number, adds 5 to it, and returns the result. The variable you pass to it doesn’t change.
Function AddFive(ByVal num As Integer) As Integer
num = num + 5
AddFive = num
End Function
Sub TestAddFive()
Dim myNumber As Integer
myNumber = 10
MsgBox AddFive(myNumber) ' shows 15
MsgBox myNumber ' still shows 10
End Sub
If you remove ByVal from the function, the second message box shows 15, because the function then changes the original variable as well.
Pass a Value with a Different Data Type
With ByRef, the variable you pass must have the exact data type the procedure expects. For example, if you pass an Integer variable to an argument declared As Long, or a Variant to an argument declared As Integer, VBA shows the “ByRef argument type mismatch” error.
ByVal doesn’t have this problem. As it passes a copy of the value, VBA can convert that value to the argument’s data type. So, if you get this error and the procedure doesn’t need to change the original variable, adding ByVal to the argument is an easy fix. The other fix is to declare the variable with the same data type as the argument.
Parentheses Pass an Argument by Value
There’s one more way to pass a value instead of the variable, and it often happens by mistake. When you put an argument in its own set of parentheses, VBA evaluates it as an expression and passes the result, so the procedure can’t change the original variable, even when the argument is declared ByRef.
'first macro
Sub MACRO1()
Dim myNumber As Integer
myNumber = 10
MACRO2 (myNumber) ' parentheses pass only the value
MsgBox myNumber ' shows 10
End Sub
'second macro
Sub MACRO2(ByRef myNumber As Integer)
myNumber = myNumber + 5
MsgBox myNumber
End Sub
Here, MACRO2 is declared with ByRef, but the second message box still shows 10. When you use the Call statement, the argument list already has parentheses, so you need a second pair to get the same effect: Call MACRO2((myNumber)). If you want a procedure to change your variable, call it without the extra parentheses (MACRO2 myNumber or Call MACRO2(myNumber)).
ByVal with Objects and Arrays
- Objects: When you pass an object (like a Range or a Worksheet) ByVal, the procedure gets a copy of the reference to the same object, not a copy of the object. So, it can still change the object’s properties, like a cell’s value or colour. What it can’t do is point your variable to a different object with Set.
- Arrays: You can’t pass a whole array ByVal. If you declare an array argument ByVal, VBA shows the “Array argument must be ByRef” error. You can pass a single element of an array ByVal, or put the array into a Variant variable and pass that Variant to a ByVal argument.
When to Use ByVal and When to Use ByRef
- Use ByVal: When the procedure only needs the value and must not change the variable you pass. It protects your variables from changes you didn’t plan for.
- Use ByRef: When you want the procedure to change the variable and send the new value back, for example, to get more than one result from one procedure.
- Large values: ByRef doesn’t copy the value, so it’s a little faster with large data types. Microsoft suggests not passing String and Variant values ByVal for this reason, though the difference is small in most macros.
Whichever one you use, write the keyword with the argument. It makes your code easier to read, and you don’t have to remember that VBA uses ByRef when you leave it out.
Frequently Asked Questions
Is ByVal or ByRef the Default in VBA?
ByRef is the default in VBA. If you don’t write ByVal or ByRef with an argument, VBA passes it by reference, and the procedure can change the original variable.
Is ByVal the Same in VBA and VB.NET?
ByVal means the same thing in both, but the default is different. In VBA, the default is ByRef, while in Visual Basic .NET, the default is ByVal. So, code you copy from a VB.NET example can behave differently in Excel VBA if its arguments don’t have the keyword.
Can I Use ByVal with an Optional Argument?
Yes. Write Optional first and then ByVal, for example, Optional ByVal myNumber As Integer = 0. But you can’t use ByVal (or ByRef) with a ParamArray argument.
Why Does My Variable Not Change After Calling a Procedure?
Either the argument is declared ByVal, or you have put the argument in its own parentheses when calling the procedure, like MACRO2 (myNumber). Both pass only a copy of the value. Remove ByVal (or use ByRef) and call the procedure as MACRO2 myNumber.
Written by
Puneet Gogia
Microsoft MVP — Excel · Founder, Excel Champs
Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.