+ Reply to Thread
Results 1 to 7 of 7

Global variable declared in Userform. Variable value is not sticking

  1. #1
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Global variable declared in Userform. Variable value is not sticking

    Hey,

    Im in big need of getting this one solved, so hope someone can help me.

    As is easier seen in the attachment, I set my variable myName to be the Combobox1.Value

    I want this value to stick with the variable, so that when I activate Sheet2, myName still equals whatever I was set to in the userform. This is not the case though, since variable value is empty when I activate another sheet. Guess it has something to do with my declaration of the variable, but cant figure it out. Please help Thanks in advance!!

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Global variable declared in Userform. Variable value is not sticking

    When you unload the userform the variable is lost. You need to declare the variable as Public in a normal module (or in Sheet2 if that is the only place you need it) instead of the userform or don't unload the form until you have used the variable.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Re: Global variable declared in Userform. Variable value is not sticking

    Thanks for your reply. I see what you mean, but how exactly would you declare the variable as you suggest? If I declared the varialble in another module, I cannot set it equal Combobox value, cause the userform is closed. Also, since I assume unloading and manually closing the userform has the same effect, I cannot activate another sheet after closing the userform without losing the variable value. Or am I missing something? Thanks again.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Global variable declared in Userform. Variable value is not sticking

    you move
    Please Login or Register  to view this content.
    to a normal module. the rest of the userform code remains the same. the variable is set by the userform and persists after you unload the form.

  5. #5
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Re: Global variable declared in Userform. Variable value is not sticking

    Very nice, that actually works! One last thing, which is nice if you could clarify also:
    In the userform code I still set myName equal ComboBox1.Value, hence I need to state Dim of myName cause I have Option Explicit on top of the userform module. But (maybe cause myName appears in the Public declaration in my other module) I get myName = "" when activating sheet2 (if I have Dim for myName in the userform code).
    I can of course survive without Option Explicit in the userform code and then it works, but if you know a way to keep O.Ex, I would be happy to hear it

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Global variable declared in Userform. Variable value is not sticking

    You should not need another Dim statement if you have declared the variable public in a normal module, even with Option explicit set.

  7. #7
    Forum Contributor timtim89's Avatar
    Join Date
    01-05-2012
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2010
    Posts
    141

    Re: Global variable declared in Userform. Variable value is not sticking

    My bad, dont know why I couldn't make it work before, cause now it does! Thank you so much for all your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1