+ Reply to Thread
Results 1 to 5 of 5

Calling commandbutton_click event from a module

  1. #1
    Forum Contributor
    Join Date
    02-20-2009
    Location
    bristol
    MS-Off Ver
    Excel 2003
    Posts
    882

    Calling commandbutton_click event from a module

    I have written the code on the commandbuttons click event and the code is present in the userform . Now Just wondering if its possible to call commandbutton_click event from the module. Like is there any way I can use from the module:

    Please Login or Register  to view this content.


    Thanks

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Calling commandbutton_click event from a module

    Hello aman1234,

    You can call code in a module from the CommandButton's Click event on the UserForm. Doing the reverse doesn't make sense. What do want to achieve?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Calling commandbutton_click event from a module

    Removed because 6StringJazzer has much better / more correct info below.
    Last edited by walruseggman; 02-26-2015 at 04:22 PM.

  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: Calling commandbutton_click event from a module

    You most certainly can call that code from any other module, but I discourage it. To do this, you must make sure it is declared as Public:

    Please Login or Register  to view this content.
    (If you create the code by double-clicking the button on the form designer, it will default to Private.)

    This is not a good programming practice. Actions taken based on user events should be distinguished from programmatic actions. In the case where you want to share a function, then it is better to create a new common Sub and call that from the form and wherever else you want to use it.

    This code goes in your UserForm module:
    Please Login or Register  to view this content.
    Then in a different Module, not the UserForm module:
    Please Login or Register  to view this content.
    And then from the other place you want to call it:
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Calling commandbutton_click event from a module

    @6StringJazzer: you're absolutely right, you can call the sub. I stand corrected and agree with your approach.

    Thanks, I learned something today!

+ 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] vba in commandbutton_click event to only allow to occur in certain cells question
    By thecdnmole in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-27-2014, 09:10 PM
  2. Calling sub in a module from another sub
    By GreggProKH in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-29-2013, 10:14 AM
  3. [SOLVED] Class Module: Identifying calling control in MouseMove event
    By tekman in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2013, 09:27 AM
  4. Calling worksheet module from other module.
    By Michael Malinsky in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2005, 04:50 PM
  5. [SOLVED] Calling an embedded items event procedure from within a normal module
    By jase in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2005, 09:05 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