Colin Legg
MrExcel MVP
- Joined
- Feb 28, 2008
- Messages
- 3,507
- Office Version
- 365
- Platform
- Windows
I kinda cheated because I've seen the thread that prompted Aaron to start his one, so I had a wee heads-up on it ....
<font face=Courier New>
' Could be declared as Sub Y(ByRef X) as well.
Sub Y(X)
X = 5
End Sub
Sub X()
Dim A As Long, B As Long
A = 1
B = 2
' Reference of A variable is passed, A is changeable by Sub Y()
Y A
' Reference of VBA internal run-time variable referenced to result of A+B is passed
' Neither A nor B is changeable by Sub Y()
Y (A + B)
' Reference of VBA internal run-time variable referenced to result of A+B is passed.
' Variable A is not passed therefore it is not changeable by Sub Y()
Y (A + 7)
' The same as above with replacing of 7 by 0 (zero)
' Reference of VBA internal run-time variable equal to result of A+0 is passed.
' Variable A is not changeable by Sub Y because expression into () brackets
' means auto creation in memory of VBA temporal internal run-time variable with
' reference to value of evaluated expression includes into () brackets.
' No special VBA-parsing of expression into the brackets is provided
' irrespective of it includes one or more variables or constants.
Y (A)
' But Call syntax means passing expression in brackets as reference.
' Reference of A variable is passed, A is changeable by Sub Y
Call Y(A)
' Reference of VBA internal run-time variable equal to result of A+B is passed.
' Neither A nor B is changeable by Sub Y as it not passed
Call Y(A + B)
End Sub
</FONT>
<font face=Courier New>
' It could be easy done by [A3]=Day(Date)
' But could you fix .Evaluate("=DAY(" & x & ")") ?
' BTW, I know how it could be fixed :)
Sub WhatIsWrong()
Dim x
x = Date
[A1] = x
With Application
' Correct
[A2] = .Evaluate("=DAY(A1)")
' Wrong
[A3] = .Evaluate("=DAY(" & [A1] & ")")
[A4] = .Evaluate("=DAY(" & x & ")")
Debug.Print .Evaluate("=DAY(" & x & ")")
' How to fix it?
[A4] = .Evaluate("=DAY(" & ??? & ")")
End With
End Sub
</FONT>