+ Reply to Thread
Results 1 to 6 of 6

Public variable used in Userform_initialize() does not work

  1. #1
    Registered User
    Join Date
    06-01-2013
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    6

    Public variable used in Userform_initialize() does not work

    The variable "Boks1" works great when submitting data Cmddata1 (green) but does not work in Userform_initialize() (red). Why?

    If I replace boks1 in Userform_initialize() with 19 it does work.


    Attached to a button in my worksheet:
    Please Login or Register  to view this content.
    My UserForm looks like this:
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Public variable used in Userform_initialize() does not work

    Declare it as Global
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    06-01-2013
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Public variable used in Userform_initialize() does not work

    Well, boks1 is declared as Public - isn't that enough? (Top of userform)
    And it won't let me change Public to Global:
    --------------
    Constants, fixed-length strings, arrays, user-defined types, and Declare statements not allowed as Public members of an object module
    -----------------

    I'm doing something wrong, I know, but I just don't know what :-)

  4. #4
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Public variable used in Userform_initialize() does not work

    Put it in a code module as a Global and then you can share it between the worksheets and the userform.
    Last edited by AndyLitch; 06-02-2013 at 08:19 AM.

  5. #5
    Registered User
    Join Date
    06-01-2013
    Location
    Europe
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Public variable used in Userform_initialize() does not work

    Do you mean like this? I believe this is the Code Module. It won't work - When I click the button it marks ".boks1 =" and sais "Method or data member not found"

    Please Login or Register  to view this content.
    Yes, I'm a newbie :-)

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,434

    Re: Public variable used in Userform_initialize() does not work

    The problem is that the initialize event is fired before the value is assigned to the variable.

    Please Login or Register  to view this content.
    If you not declare it in a standard code module as AndyLitch suggests you will need to create your own initializing event in order to set things up with correct values befoew the use form is shown

    userform code
    Please Login or Register  to view this content.
    module code
    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

+ 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