The following code produces different results based on how the ChangeString
sub is called.
- If I use
ChangeString (str)
, the message box returns "First String". - If I use
ChangeString str
orCall ChangeString(str)
it returns "Second String".
They all should me equivalent ways to call a sub and pass it an argument, so I expected to get "Second String" every time.
I tried the code on both Excel 2016 and 2021.
Sub TryByRef()
Dim str As String
str = "First String"
ChangeString (str)
'ChangeString str
'Call ChangeString(str)
MsgBox str
End Sub
Sub ChangeString(ByRef s As String)
s = "Second String"
End Sub