+ Reply to Thread
Results 1 to 13 of 13

Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    I have
    Please Login or Register  to view this content.
    The sub (parsed down for brevity) is called whenever a button click event occurs within a userform. I have many userforms where this sub could conceivably be called when a date picker button is clicked. I know I could just rewrite the routine to Sub 1, Sub 2, Sub 3, etc. and reference those as Call Sub1, Call Sub2, etc. but I'm more interested in how I can make this If statement reference whatever form the sub is being called from, and have the variable declaration for the control within the click event suchas:
    In Userform(n):
    Please Login or Register  to view this content.
    In Sub1 in standard (class?) module:
    Please Login or Register  to view this content.
    Last edited by terriertrip; 09-27-2017 at 02:35 PM.

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

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    You'd use a class, but we'd need to see an example of how you're calling these forms along with a couple of forms. Could you upload a workbook?

  3. #3
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    Thanks, Kyle. That's what I sort of suspected. I've decided to go another direction with it.

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

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    Ok, you realise it'll only be a couple of lines of code though right?

  5. #5
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    here's a sample. I tried removing the module and directly inserting it into the click event, but their is some connection to the function, so it errors out. If you've got any ideas, I'm all for that.
    Attached Files Attached Files

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

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    The easiest way is to simply turn your sub into a function:
    Please Login or Register  to view this content.
    Then in userform1:
    Please Login or Register  to view this content.
    Userform1 and 2 shouldn't be different userforms either - you should be using the same one

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    Or you put it straight into the CalendarButtonClick-event. (as I pointed out in your previous thread)

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

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

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    @bakerman2 you wouldn't if you want the datepicker in multiple places though with the same config

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    @ Kyle123

    I would put these standard values straight in the datepicker function because I don't think you'll be changing all these settings every time U open the calendar form.

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

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    Indeed, but it makes it less portable. As it stands the useform is both portable and configurable, moving configuration into the calendar userform limits these options without faffing on future projects.

    Not wanting to change them is an argument for putting the config somewhere else and follows DRY principles - that's why I suggested calling the DatePicker function from a module - the config will be the same regardless of what is calling it

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    It's a bit beside the scope of this thread so I'll keep it with this last remark.

    From the makers point of view I agree with you totaly regarding portability. But when you, as an individual, download the CalendarForm you set it up once to your liking and that's it.
    You don't go saying that on Monday I want a red background, on Tuesday a grey one,... and so on. So I would test somethings out and once it's completely to my liking I would set them solid in the From itself.
    That's how I would do it, of course.

  12. #12
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    Their different forms because there are a number of forms for different tables that require date inputs. That was simply the mockup to see if different subs could call the same routine.

  13. #13
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    Re: Excel - VBA : pass variable from Sub to Userform (Date Picker) Part II

    Kyle123 - It works on different forms. Awesome. Thanks for the code!

+ 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] Excel - VBA : pass variable from Sub to Userform (Date Picker)
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-27-2017, 02:23 AM
  2. Date picker, userform and other problems - Excel for mac
    By dmilanb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-01-2016, 12:53 PM
  3. adapting a downloaded, excel date picker form into my own userform
    By HeyInKy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-11-2014, 03:50 PM
  4. Date picker control for Excel 2003 userform
    By GreyGhost in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-19-2014, 06:20 PM
  5. Excel query problems with VBA/ADO to iSeries... date formatting, pass variable w/ quotes
    By anthallen_dps in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-04-2013, 05:08 PM
  6. [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