+ Reply to Thread
Results 1 to 5 of 5

Calling a public function from userform

  1. #1
    Registered User
    Join Date
    07-09-2009
    Location
    New York, NY
    MS-Off Ver
    Excel 2003
    Posts
    2

    Calling a public function from userform

    I apologize in advance if this is a dumb question, or asked previously. I tried searching (and Googling) to no avail.

    I was wondering how I can call a function (with 4 parameters, and returns a String) defined in the ThisWorkBook tab of my VBE, and call from a userform (specifically, when I click a button on the userform, I want it to use the public function to calculate something). I wanted to keep it separate because I will need to access the function from a 2nd userform later.

    Thanks in advance!

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

    Re: Calling a public function from userform

    It sounds as though the function really belongs in a normal module. If you put it there, you should be able to call it from the userform as usual. If you need to keep it in the ThisWorkbook module, then you would need to prefix the function call with ThisWorkbook:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  3. #3
    Registered User
    Join Date
    07-09-2009
    Location
    New York, NY
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Calling a public function from userform

    I've actually tried using the
    Please Login or Register  to view this content.
    method, but again, it doesn't work. I've also tried putting the function in a module, also, no luck.

    Let me go into a little more detail about what the function is.

    I have a function that I copied from the web somewhere, that reads a string and the length it ought to be. Then it decides if the string needs to be padded by spaces to satisfy the length requirement (or truncate the string if it's too long).

    I have a userform that gets some text from a user, and when I click a button, I want it to take in the input text, run it through the function for spacing and padding, and put it on a worksheet in the same workbook.

    If I have to, I'll paste the function code in the userform tab but I think that's redundant if I have to access it from another userform later.

    I'm a newbie at VB programming, learning everything from google and experimentation, and an ex-Java programming student 5 years ago, so maybe I'm missing something.

    Any other ideas? Thanks in advance.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Calling a public function from userform

    Stomper's suggestion works fine. If the code is in a code module (where it probably should be), then just
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

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

    Re: Calling a public function from userform

    Is the function in the same workbook as the userform? If not, you wouldn't be able to call it directly - you would need to use Application.Run or, if it's a public method of the other ThisWorkbook object, you should be able to use:
    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)

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