+ Reply to Thread
Results 1 to 8 of 8

Passing a variable to a userform

  1. #1
    Registered User
    Join Date
    08-19-2012
    Location
    Townsville
    MS-Off Ver
    Excel 2010
    Posts
    6

    Passing a variable to a userform

    I have a “flat” list of corrective actions in one sheet (ActionTotal), that are assigned to various people. Each action (row), has a unique number (“Ref”).
    There is a userform (frmActionClose) that accepts a number as input in a textbox on the form (“txbACActionNumber”), finds the specific record, and if the “Date Closed” date is entered, closes the action for that person. To close an action requires that you know the “Ref” number for that record.
    I have another sheet, with a pivot table, that will show all the open actions for one person. I would like to be able to select the “Ref” number on that sheet (“MyActions”), and pass that number to the Userform, so that the actions can be closed without having to have a separate list of “Ref” numbers.
    How do I pass this number to the userform “frmCloseActions”, so that it uses the value as the value of the “txbACActionNumber”? I can read the value from the form easily enough, and open the userform, but where to then?

    Thanks in advance
    OldMan1950

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Passing a variable to a userform

    Hi OldMan62,

    You have to save the value somewhere that is accessible to the UserForm. Two ways I have done this is to:
    a. Save the value in a cell on the Worksheet. You may have to clear that cell after you are done using the value.
    b. Save the value in a global variable. For example, in an ordinary code Module:
    Please Login or Register  to view this content.
    'myGblVariable' can be used to store/retrieve the value from any macro. Notice how the global variable is declared AFTER 'Option Explicit' and BEFORE the first Macro.

    Lewis

  3. #3
    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: Passing a variable to a userform

    LJMetzger's solutions work perfectly well. Another solution that I use for software engineering reasons is to create your own Show procedure (this is called overloading and is legal in VBA) and pass your number as an argument.

    In frmCloseActions, the code would look like
    Please Login or Register  to view this content.
    Then to open the form you call Show with the argument:
    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Passing a variable to a userform

    If you have selected your reference. Then just tell the userform to read the active cell value.


    Please Login or Register  to view this content.

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Passing a variable to a userform

    Quote Originally Posted by 6StringJazzer View Post
    Another solution that I use for software engineering reasons is to create your own Show procedure (this is called overloading and is legal in VBA) and pass your number as an argument.

    In frmCloseActions, the code would look like
    Please Login or Register  to view this content.
    Then to open the form you call Show with the argument:
    Please Login or Register  to view this content.
    Jeff,
    You can't overload the Show method of a userform in VBA - it simply won't compile. Can you post a workbook demonstrating this? I'd be very interested to see it.
    Remember what the dormouse said
    Feed your head

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Passing a variable to a userform

    Hi,

    You can use properties also, so you can say

    Please Login or Register  to view this content.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  7. #7
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Passing a variable to a userform

    I made a couple of small changes...

    Please Login or Register  to view this content.

    Sent from my iPhone using Tapatalk

  8. #8
    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: Passing a variable to a userform

    Quote Originally Posted by romperstomper View Post
    Jeff,
    You can't overload the Show method of a userform in VBA - it simply won't compile. Can you post a workbook demonstrating this? I'd be very interested to see it.
    I'm sorry, absolutely right. It has been a while since I've done that and I got my languages confused. What I actually did was created a new Sub, rather than overloading.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

+ 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. Userform not passing through 1 particular public variable
    By newbvba in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-20-2014, 09:59 PM
  2. passing variable from userform
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-31-2010, 08:22 PM
  3. Passing Variable from Sheet to Userform
    By gti_jobert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-27-2006, 05:38 AM
  4. Passing variable from userform to module error
    By mworth01 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2005, 12:47 PM
  5. [SOLVED] Passing variable to VarType of other Variable
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2005, 10:06 AM

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