+ Reply to Thread
Results 1 to 12 of 12

userform to override a variable in a module?

  1. #1
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    userform to override a variable in a module?

    I am wondering if it is possible to have a textbox in a UserForm and upon pressing a Run button, for the UserForm to go to a certain macro (specified in the button Run code) and override a selected variable with the value entered in the userform textbox?

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: userform to override a variable in a module?

    Example: I have a macro that runs automatically when I press a button on my worksheet, and it uses "10" for a variable called "period".

    Can I run this same macro, and override that "10" and set the variable "period" to a value entered in a UserForm textbox?

  3. #3
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: userform to override a variable in a module?

    Hi

    You need to set period as an optional argument to the macro you call eg:

    Please Login or Register  to view this content.
    Then when you call this routine from the userform, you can insert the value from the textbox:

    Please Login or Register  to view this content.
    Richard
    Richard Schollar
    Microsoft MVP - Excel

  4. #4
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: userform to override a variable in a module?

    Please Login or Register  to view this content.

    How does it know we're talking about "Period" variable? I get everything, I just want to make sure how we're passing the "Period" here. I see that we're passing something from Textbox1, but to what? Does it automatically guess it's to "Period" because it is an "Optional" variable?

    Thank you very much for your help! This will save me some serious time!

  5. #5
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: userform to override a variable in a module?

    In my example, MyMacro only takes at most one argument so the VBA compiler knows that any argument to the sub is only ever going to be a value for Period. You do have to pass a value of the required type (in this instance a Long) but the Val() function should ensure this is the case.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,289

    Re: userform to override a variable in a module?

    Val actually returns a Double, so technically you should CLng it...
    Remember what the dormouse said
    Feed your head

  7. #7
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: userform to override a variable in a module?

    Yeah but VBA's internal conversions handle that point as you well know. It's a way of handling if text has been entered too.

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,289

    Re: userform to override a variable in a module?

    Quote Originally Posted by RichardSchollar View Post
    You do have to pass a value of the required type (in this instance a Long) but the Val() function should ensure this is the case.
    I'm just being pedantic (for a change) since it's actually the implicit conversion, not Val, that ensures you get the right type. And as we all know, even if we ignore it most of the time, an explicit conversion is better than an implicit one.

    Only a couple of hours until beer now though...

  9. #9
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: userform to override a variable in a module?

    Quote Originally Posted by romperstomper View Post

    Only a couple of hours until beer now though...
    Deleted and PM sent

  10. #10
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: userform to override a variable in a module?

    Hope you get better soon, Richard.

    Hope the little ones are all doing well
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  11. #11
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: userform to override a variable in a module?

    so how do i pass two variables then?

  12. #12
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264

    Re: userform to override a variable in a module?

    You just need to include them in the Sub (or Function) declaration eg:

    Please Login or Register  to view this content.
    Then call that:

    Please Login or Register  to view this content.
    Any optional arguments to the MyMacro must be listed at the end of the arguments list.

+ 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