by val explination

chris2727272727

Board Regular
Joined
Jul 10, 2005
Messages
152
(ByVal Target As Range, Cancel As Boolean)
(ByVal Target As Range, Cancel As Boolean)
(ByVal Target As Range, Cancel As Boolean)

(ByVal Target As Range)

What do the above expressions mean???
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

tusharm

MrExcel MVP
Joined
May 28, 2002
Messages
11,028
For simple variables (i.e, not objects) the below test should illustrate the difference between ByVal and ByRef
Code:
Option Explicit

Sub testByRef(ByRef x As Integer)
    x = x - 1
    End Sub
Sub testByVal(ByVal x As Integer)
    x = x - 1
    End Sub
Sub testControl()
    Dim y As Integer
    y = 10
    testByVal y: Debug.Print y
    testByRef y: Debug.Print y
    End Sub

Basically, ByVal makes a copy of the variable (using its current value) and passes that copy to the subroutine. So, any changes the subroutine makes to the argument are made to the copy of the actual variable. Since the copy is discarded when the subroutine exits, the original remains untouched.
 

Watch MrExcel Video

Forum statistics

Threads
1,118,910
Messages
5,574,989
Members
412,633
Latest member
simon_elvin
Top