LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Disappearing Public Variable

Many thanks semiopen - that was the problem.
--
donwb


"semiopen" wrote:


donbowyer wrote:
Hi again Dave
This is most strange.
I've tried a very stripped down version of my code bur the problem persists.
I have a UserForm (with OK, Cancel and a TextBox) and a Standard Module.

The Module code is:-
""Public MyText
Sub MyAddSheet()
UserForm1.Show
MyHold_1 = MyText
MyHold_2 = UserForm1.TextBox1.Text
End Sub""

The UserForm code is:-
""Private Sub CommandButton1_Click()
Dim MyText As Variant
MyText = UserForm1.TextBox1.Text
Unload UserForm1
End Sub""
The module code takes me to the UserForm. I insert text into TB1 and click OK.
As I step through, the text I put in TB1 appears in the Locals window as a
value under my Public Variable MyText.
When control returns to the module code, MyHold_1 says "Empty" and MyHold_2
says "" in the Locals window.
In other words the value in the variable disappears when control leaves the
UserForm.
?????

--
donwb


You have created a local variable in your user form which is hiding (or
"shadowing") the global variable. The user form loads the data into
*its* MyText - which goes out of scope when that routine is done. The
solution is to just get rid of the line

Dim MyText As Variant

Which should fix everything.

-semiopen


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set Public Variable on Open jlclyde Excel Discussion (Misc queries) 4 September 11th 09 07:03 PM
Public variable Jack New Users to Excel 4 March 18th 06 09:35 PM
How to declare variable as public. Mark Excel Programming 3 April 7th 05 06:27 PM
Public Variable Jason Excel Programming 4 April 12th 04 07:06 PM
public variable marwan hefnawy Excel Programming 1 September 5th 03 08:54 AM


All times are GMT +1. The time now is 10:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"