tiredofit
Well-known Member
- Joined
- Apr 11, 2013
- Messages
- 1,924
- Office Version
- 365
- 2019
- Platform
- Windows
I am a bit confused with regard to the Lifetime and Scope of variables.
Given this code:
After running Sub FromSheet1 through to End Sub, if I go to the immediate window and type:
I get a blank, which suggests to me the variable a has been cleared out because Sub FromSheet1 has finished running.
But if I subsequently run Sub FromSheet3 and put a break point as indicated, then go to the immediate window, it somehow "remembers" that a = 1.
Is this because the variable a has been declared at the module level?
Thanks
Given this code:
Code:
Option Explicit
Dim a As Integer
Sub FromSheet1()
Sheet3.Select
a = 1
End Sub
Sub FromSheet2()
Sheet3.Select
a = 2
End Sub
Sub GoBack()
If a = 1 Then Sheet1.Select ' PUT BREAK POINT HERE
If a = 2 Then Sheet2.Select
End Sub
After running Sub FromSheet1 through to End Sub, if I go to the immediate window and type:
Code:
? a
I get a blank, which suggests to me the variable a has been cleared out because Sub FromSheet1 has finished running.
But if I subsequently run Sub FromSheet3 and put a break point as indicated, then go to the immediate window, it somehow "remembers" that a = 1.
Is this because the variable a has been declared at the module level?
Thanks