+ Reply to Thread
Results 1 to 11 of 11

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

  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)

    I want to pass a variable from a sub of my Module1 to a userform. Right now the value is put into the active sheet. The sub is called when the user clicks a textbox (the very textbox I would like to return the value after running the sub). Is this possible? Just curious.
    Please Login or Register  to view this content.
    Last edited by terriertrip; 09-26-2017 at 09:17 PM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Excel - VBA : pass variable from Sub to Userform

    Is it
    Please Login or Register  to view this content.
    ?

  3. #3
    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

    How about ?

    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.

  4. #4
    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

    Yeah I tried both of those, but no luck so far. I think it maybe close though. Unusually when the sub is called the first time, it immediately produces type mismatch error for
    Please Login or Register  to view this content.
    When I reset and call the sub from the userform1 a second time, userform2 loads, however when the getdate function is executed,
    Please Login or Register  to view this content.
    bombs out here 'form already displayed; can't show modally. I've tried .show 0 and .show 1 but no luck.
    Do I need to ensure that userform when initialized is modeless?

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Excel - VBA : pass variable from Sub to Userform

    Is datevariable getting corret result?
    I'm out so, can't test

  6. #6
    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

    Afraid not. I'll load a sample wk tomorrow.

    Edit: make that today.

  7. #7
    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

    I've posted a sample workbook. I've decide to add a small calendar button to the textbox side. I will disable the textbox once we can get the calendar sub to work so the user can only change or add the date through the date picker.
    Attached Files Attached Files

  8. #8
    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

    Try this. Select calender button to select date. (Calendar form starts up with current date selected)
    Result is put in disabled Textbox2.
    Attached Files Attached Files

  9. #9
    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

    Interesting. I was thinking along the same lines today with the calendar button, and in fact started building them into my forms with the hope that that would be the solution. Imagine my surprise to see this! Works great. Thanks bakerman2!

  10. #10
    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)

    You're welcome and thanks for rep+.

    BTW Any particular reason why you would call the userform through an outside sub as you can put the datevariable-code directly in the calendarbutton-click event.
    No outside module needed then.

  11. #11
    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)

    No problem. Thank you for the help!

    Good point. Just keep it all in one routine. I'll have to save looking at class modules and object events for another day.

+ 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. Update record - how to pass variable from one userform module to another
    By Rob K in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-11-2015, 09:49 AM
  2. Replies: 3
    Last Post: 05-15-2015, 07:27 PM
  3. How to pass a variable value from a Userform listbox/spinbutton to another sub
    By max3732 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2014, 01:01 PM
  4. 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
  5. Pass Variable From UserForm to Module
    By BLLMRC in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-24-2013, 12:41 PM
  6. [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
  7. [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