
Originally Posted by
Pan314
I attach a file. I hope this will be understood.
Is not explicit declaration in the file. I have no idea to this.
Further explanation is in the file.
This code is different than the first code you posted.
Here is the code in ThisWorkbook:
Private Sub Workbook_Open()
stringInSubWorkBook_Open = "testString" 'stringInSubWorkBook_Open-called variable, which must be accessed in the modul1.
Call TestInModul1 'Here it is only for the purpose of testing
End Sub
Here is the code in Module1. Note my comment in red.
Public Sub TestInModul1()
Select Case stringInSubWorkBook_Open
Case ""
MsgBox "stringInSubWorkBook_Open is out of context."
Case Else
'Here I should definitely have access to the stringInSubWorkBook_Open variable
Absolutely not. stringInSubWorkBook_Open is not explicitly declared,
so VBA considers it to be implicitly declared inside Sub Workbook_Open because that is where it is used.
If you review my visibility rules you will see that this variable cannot be visible outside the Sub.
In your first example, it was declared outside that Sub and could be made visible with
qualification as shown in red in my code example.
Further, it is not declared in TestInModul1 either, so it is also declared implicitly there.
You have two different variables in two different Subs that happen to have the same name.
'It is now only available in the ThisWorkbook
MsgBox stringInSubWorkBook_Open '
End Select
End Sub
You should use Option Explicit and always require that all variables are explicitly declared. It prevents bugs like this.
Bookmarks