byval by ref vba code example

Example: vba byref byval

Sub TestMe()
    Dim sString As String
    sString = "Initial value"
    
    Call EstimVal(sString)
    Debug.Print sString          ' "Initial value"

    sString = "Initial value"
    Call EstimRef(sString)       ' ByRef with call
    Debug.Print sString          ' "ByRef"
   
    sString = "Initial value"
    EstimRef (sString)           ' ByRef without call
    Debug.Print sString          ' "Initial value"
   
    sString = "Initial value"
    Call EstimRef((sString))     ' ByRef with call and (): sString evaluated first
    Debug.Print sString          ' "Initial value"
End Sub
'--------------------------------------------------------------
Function EstimVal(ByVal pVar As String)
   pVar = "ByVal"
End Function
Function EstimRef(ByRef pVar As String)
   pVar = "ByRef"
End Function

Tags:

Vb Example