Dim vs. Public
Jason Morin wrote:
I was under the impression that if I wanted to
share variables between macros, I had to declare them
Public, even if all the macros were in the same module.
You can share values between sub procedures by passing values as
parameters. This way, you can give some thought as to whether those
values/objects can be passed ByVal e.g.
Private Function AddTen( _
ByVal Value As Long _
) As Long
AddTen = Value + 10
End Function
Private Function SwitchValues( _
ByRef Value1 As Long, _
ByRef Value2 As Long _
) As Long
Dim lngTemp As Long
lngTemp = Value1
Value1 = Value2
Value2 = lngTemp
End Function
Module-level variables are for persisting values/objects between top
level calls. Declare them as Private; Dim is supported at the module
level for backwards compatibility only.
As far as I know there is no good reason for using a Public variable;
I've never used one myself. If you need to share a module-level
variable with another module, you can use a Property, which should
similarly prompt you to consider whether the value can be read only
(Property Get only) or read-write (additional Property Let or Set).
Jamie.
--
|