2

Is it possible to have a 'public' variable and 'dim' variable with the same name and use both in the same sub?

1 Answer 1

4

The more local variable wins (shadowing).

To access the variable in the global scope you would need to qualify it with its containing module name:

Public Foo As Long

Sub g()
 dim Foo As String
 ...
 msgbox Foo
 msgbox ModuleName.Foo
Sign up to request clarification or add additional context in comments.

5 Comments

Can't accept the answer for 5 more minutes but as soon as I can you got it, the hierarchy is Public, Private, Dim for scope am I missing anything else?
There is also Static and Friend although the latter is for class methods only
There is also Global that I think is even above Public.
Is there an MSDN documentation page on Scope, it seems strange that they have documentation for all the functions but not the basics of the language itself.
For office 2013 the language reference is in the Office 2013 shared section

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.