'Global variable loses value after userform

I know there're several similar questions out there, though I'm afraid none of their answers solve my problem.

I created a simple file to test the problem. Code in Sheet1:

Public aaa As String

Sub abc()
    UserForm1.Show
    Debug.Print aaa
End Sub

Code in UserForm1:

Private Sub CommandButton1_Click()
    aaa = TextBox1.Text
    Debug.Print aaa
    Unload UserForm1
End Sub

I run the code line by line, the Debug.Print before Unload works fine; and the public var loses its value right after the userform is unloaded. I've tried to change Unload to Hide, didn't work. The value of the global var loses anyway.

I thought this test file is simple enough to avoid any problem...can anyone tell me why the global var cannot keep its value? Thank you.



Solution 1:[1]

In my case I had left variable with same name inside module and the other module, that initializes global variables. Hopefully will help some lost soul.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yomajo