+ Reply to Thread
Results 1 to 6 of 6

Pass variables from userform to module

  1. #1
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Pass variables from userform to module

    Hi again,

    last thred from me regarding userforms etc.

    I have a quite complex (in my opinion) userform in a sheet. It contains checkboxes, textboxes and other form controls. I have a "go" button as well.

    When the "go" button is activated, I would like to pass all the variables (the states of checkboxes etc.) to a module and execute the code within it.

    As example: Let's say i have 3 checkboxes - depending on whether the user has ticked them or not, I would like to run a macro which would select either "A1", "A2", "A3" or any combination of those three.

    How do I proceed here? I don't want to ask anyone to create me a working macro for my example. I'm merely looking for a easy way to pass the info of a userform to the module.

    PS. I know I could bind the variables to a range in a worksheet, but I'd like to make it work in a more "sophisticated" way.

    Thanks
    If you think that my answer was helpful, please click on the "Add to this user's Reputation" button.

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Pass variables from userform to module

    Why not have the code in the userform rather than a module?

  3. #3
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Pass variables from userform to module

    Thanks for your reply Kyle123!

    That's another way to do it. I just wanted to keep the workbook clean - userforms in userforms, fancy code in modules. If there is no other (easy) way to pass the info to a module, then I'll stick to the userform.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Pass variables from userform to module

    Well you can pass variables in functions, or by having public variables, you can also call a module and refer to the form through it's object - there are drawbacks to each, all of which make things more complicated

    You'd be better putting the code that the userform uses in the userform, it makes it much easier to follow when you come back to it in 6 months and have forgotten how it works

    You won't have to search all over your workbook then just to follow your code and find out what something does

    It's better practice to keep things self contained, use your modules for functions and subs that need to be called from more than one place.

  5. #5
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Pass variables from userform to module

    then userform it is! Thanks!

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Pass variables from userform to module

    Glad I could help, thanks for the rep

+ 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