+ Reply to Thread
Results 1 to 10 of 10

Pass Variable From UserForm to Module

  1. #1
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    8

    Pass Variable From UserForm to Module

    Hi,

    how to declare an integer variable (calculated into a userform) to allow it to pass from a userform to a module?

    Thank you

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Pass Variable From UserForm to Module

    Try declare the variable as public variable

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Pass Variable From UserForm to Module

    What does the code in the module do?

    Are you calling a sub in the module from the userform?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Pass Variable From UserForm to Module

    I've tried to declare it as Public but in a userform doesn't work.
    The code in the userform takes 3 values from 3 different textboxes (day, month, year) and define the date as general number. I need to use this number in another module. If the user insert something wrong a an error message is displayed and the .Redoaction command allows the user to try again. If the the input it's correct the userform code will run the Sub placed into the Module.

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

    Re: Pass Variable From UserForm to Module

    declare it as public in a normal module, or pass it directly to the routine you call from the form
    Josie

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

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Pass Variable From UserForm to Module

    You can pass the date to the other sub like this, which would go in the userform.
    Please Login or Register  to view this content.
    The other sub would need to be adjusted to take the argument.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Pass Variable From UserForm to Module

    Thank you Norie,

    I'm going to have a go.
    I know it sounds really really bad but for the moment I've just written my variable in a hidden label caption. In the sub I'm "calling" the value from the label caption and I'm converting it into a number again. At least it works..

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Pass Variable From UserForm to Module

    I was actually going to suggest something like that but didn't know how you were calling the form or the sub.

    Actually, do you need a hidden label? Can't you take the values directly from the textboxes on the form?

  9. #9
    Registered User
    Join Date
    06-11-2013
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: Pass Variable From UserForm to Module

    It's just a label without caption. As soon as I write the new caption I hide the userform.
    Actually the first attempt was to run the sub directly in the userform as part of its code, which sounds like your idea.
    But for the first time (and I don't know why) if an error was found and the vbCritical messagebox displayed, the code was simply "passing through" the Me.Redoaction running in any case the rest of the code. So the user didn't have the chance to correct the mistake. It sounds silly but that's why I've decided to separate Module and Userform.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Pass Variable From UserForm to Module

    If you can get the caption of a label from the userform you can get the values in the year, month, day textbox too.

    By the way, I wasn't suggesting having the sub in the userform code, thought that might be a good idea.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Pass date variable to another procedure/module
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-20-2013, 10:16 PM
  2. [SOLVED] Pass variables from userform to module
    By Bishonen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-08-2012, 07:54 AM
  3. Pass variable between subs within module
    By oOarthurOo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-01-2010, 04:58 PM
  4. [SOLVED] Pass variable from module to userform and back
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-20-2006, 11:50 AM
  5. Pass combo box value to string variable in module
    By magix in forum Excel General
    Replies: 1
    Last Post: 12-15-2005, 11:50 AM

Tags for this Thread

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