+ Reply to Thread
Results 1 to 13 of 13

How to use a function in more than one userform

  1. #1
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    How to use a function in more than one userform

    Hi, someone can help me with a tip, please.

    How can I use One or Two functions inside two or more userforms(let's suppose this project it will be grow more then 3 Userforms)?
    I have Userform1 and Userform2 each one has texBox named --> txtData


    If I put this two functions into a Module, how pass textbox to this function, who textbox is from Userform1 or Userform2?
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Thank you
    "No xadrez nem sempre a menor dist?ncia entre dois pontos ? uma linha reta" G. Kasparov.

    If your problem is solved, please say so clearly, and mark your thread as Solved: Click the Edit button on your first post in the thread, Click Go Advanced, select b from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Edit button will not appear -- ask a moderator to mark it.

  2. #2
    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: How to use a function more than one useform

    In your UserForm module you simply call the function. You are not specific about what the flow is that you have designed for your user, but you can use code like this in any UserForm module:

    Please Login or Register  to view this content.
    It doesn't matter what the next of the textbox is. The code above is for txtData but you can change it to anything in different UserForm modules.

    (BTW you should have comments explaining what these functions do. At least describe the expected inputs, outputs, and error conditions.)
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: How to use a function more than one useform

    Hi, look at my file.

    Has Two UseForm
    1 - frmProducao
    2 - frmEntradaPRF

    Modules
    1 - Mod_Utils

    Inside it, Two functions (I'm trying use this in a module instead of UserForm module)
    1 - IsCorrectDate()
    2 - GetUSDate()

    Thank you
    Attached Files Attached Files
    Last edited by marreco; 09-12-2021 at 06:53 AM.

  4. #4
    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: How to use a function in more than one userform

    The way you have used IsCorrectDate is correct. You are using it correctly in both forms.

    (I find the function itself confusing as a user. When I select the Data textbox and type "10/1/2021" it shows "10/1" and will not allow any more characters. If you want the user to use a specific date format like MM/DD/YYYY then you should show that on the UserForm.)

    You can use GetUSDate the same way, but I don't know what you want to do with this. I don't know how you are using your forms or what you want to do with the "Data" textboxes.

  5. #5
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: How to use a function in more than one userform

    Hi

    If I try use this function out of UserForm module, this error show up:
    Runtime Error 424 “Object Required”
    I try to reuse a function in all my project if necessary, I mean use all Userforms in project.

  6. #6
    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: How to use a function in more than one userform

    I did not get this error. Please give the exact steps you took to get the error.

  7. #7
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: How to use a function in more than one userform

    Hi Try my edited file(MyFile_V2.xlsb) in #3

  8. #8
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: How to use a function in more than one userform

    Hi
    Any idea?

  9. #9
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,222

    Re: How to use a function in more than one userform

    You needlessly waste energy checking that the date is correct. The pest-user will defeat you someday anyway.
    You can win with him without giving him the option to create the date in any way you want. He'll get a calendar. And then nothing will spoil you.
    Spend the time recovered in this way to validate data transferred from forms to sheets. I marked the incorrectly entered data in the Producao sheet.
    BTW. Do not use diacritics in the names of procedures and variables, even if Excel allows it. This often causes problems.
    And two simple rules for handling forms:
    1. Data from sheet to control - enforce data format.
    2. Data from control to sheet - enforce data type.

    In attachment, the version with inserted calendar.

    Artik
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: How to use a function in more than one userform

    Hi Artik, this will help me a lot, thank you.

    But my point is... imagine that you have function One(), and function Two(), and use it inside that all userforms instead write same (function One, function Two) inside each UserForm.


    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,222

    Re: How to use a function in more than one userform

    The IsCorrectDate function is misspelled on this line:
    Please Login or Register  to view this content.
    It should be:
    Please Login or Register  to view this content.
    But please don't go that way. Even after the function is repaired, i am able to fool the current procedures in several ways and allow the wrongly constructed date to be saved. There are many memes on the internet about how programmers perceive users. Most often with a club in hand. And it is true, unfortunately.

    Artik

  12. #12
    Valued Forum Contributor marreco's Avatar
    Join Date
    07-02-2011
    Location
    Brazil
    MS-Off Ver
    Excel 2010
    Posts
    1,862

    Re: How to use a function in more than one userform

    Last question...
    How to user UserformCaledar to display in my date and liguage?

    This...
    Day of the week abbreviation
    Monday Mo.
    Tuesday Tu.
    Wednesday We.
    Thursday Th.
    Friday Fr.
    Saturday Sa.
    Sunday Su.
    To this
    Dia da semana abreviação
    Segunda Se
    Terça Te
    Quarta Qu
    Quinta Qu
    Sexta Se
    Sabado Sa
    Domingo Do

  13. #13
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,222

    Re: How to use a function in more than one userform

    I did not notice that the code was localized into Polish.
    In the attachment, the form module itself (2 files), in which I corrected the code so that the names of the months and the abbreviated names of the days of the week were displayed in the local language. In the original form, three texts are permanently assigned: the title of the form, the heading of the week numbers, and the text of the selected date. The header and the text are visible only after passing the appropriate arguments to the GetDate function. They are not visible in the default version. I tried to translate these 3 texts. This happens in the GetCaption function. If the texts are not displayed as expected, the function code must be adapted to your own system settings.
    How to import a module to your own project?
    1. Make sure your project does NOT contain the CalendarForm module.
    2. In the VBA editor, select File / Import File (or the keyboard shortcut Ctrl + M) and select the * .frm file.

    Artik
    Attached Files Attached Files

+ 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 function
    By aparunkumar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2016, 05:41 AM
  2. userform function
    By aparunkumar in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2016, 05:26 AM
  3. [SOLVED] Userform won't function.
    By RinorM in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-22-2015, 04:21 PM
  4. [SOLVED] Function Key To Load UserForm
    By SheppeyMan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-01-2014, 06:54 PM
  5. [SOLVED] Problem with userform function
    By Phouthavongc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2013, 03:34 PM
  6. [SOLVED] Using If function with VBA UserForm
    By eemiller1997 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-12-2012, 08:55 PM
  7. Userform Search Function Autofilter Results and Repopulate Userform
    By cindy71 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-16-2012, 03:46 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