+ Reply to Thread
Results 1 to 4 of 4

How to pass a variable value from a Userform listbox/spinbutton to another sub

  1. #1
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    How to pass a variable value from a Userform listbox/spinbutton to another sub

    I have a listbox populated with the 12 months and under the "OK" button I increase the selected value by 1, so if they've clicked on January it stores "1", June it stores "6", etc. There is also a spinbutton and textbox where I have "2014" and lets the user increase this by one.

    How do I pass this numbers from the listbox and textbox to a sub in module 1? I have a sub that opens a workbook and I need the date to figure out which workbook to open. The workbook is named in the same basic format each month of "2014-05" for May 2014.

    I've tried making the sub in the userform public, naming the varaibles public and just can't figure it out.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: How to pass a variable value from a Userform listbox/spinbutton to another sub

    Sorry but this is an incomplete description:

    under the "OK" button I increase the selected value by 1, so if they've clicked on January it stores "1",
    Increase what selected value? Stores it where?
    naming the varaibles public
    What variables? You haven't said anything about variables in your description.

    Based on what you have described, there are two ways to do this. The simplest is to refer to the listbox and textbox in the sub. You don't give the name of your form so I'm going to call it UserForm1, and so on for the other objects.

    Please Login or Register  to view this content.
    The other more complicated way is to declare public variables in your UserForm code, and assign them as above when the user clicks OK. Then refer to those variables in the other Sub. This breaks the dependency so that Module1 no longer has to know how UserForm1 has implemented the controls (which is an advantage).

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    04-01-2010
    Location
    USA
    MS-Off Ver
    Office 2021
    Posts
    185

    Re: How to pass a variable value from a Userform listbox/spinbutton to another sub

    Thank you so much. It's been a while since I did anything with userforms and I didn't realize you could access them like you did in the first example. The 2nd method looks like it would be helpful in many situations.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why not open the workbook(s) with code in the userform?
    If posting code please use code tags, see here.

+ 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. Can't pass a variable value from a worksheet sub to a userform sub
    By Dan Vollmer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-24-2014, 10:24 AM
  2. Pass Variable From UserForm to Module
    By BLLMRC in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-24-2013, 12:41 PM
  3. Pass values from UserForm ListBox/ TextBox to run the codes of a Module
    By SunOffice in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-05-2011, 08:59 AM
  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. [SOLVED] Pass public variable from one userform to a second...
    By Mike Dunworth in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2005, 08:05 PM

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