+ Reply to Thread
Results 1 to 10 of 10

Call button click event from a button on anther sheet

  1. #1
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Call button click event from a button on anther sheet

    Hi

    I have a sheet1 with a command button with some code in its CommandButton1_Click() procedure (or is it called event? ).

    I have another button (cmdResource) on another sheet (sheet2) in the same excel file. Upon clicking this button, I want to execute the code of sheet 1's CommandButton1_Click().

    How can I achieve this?

    tia
    ajay
    Last edited by ajaykgarg; 03-25-2010 at 09:27 AM.

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

    Re: Call button click event from a button on anther sheet

    If you move the code to a public routine in a normal module, you can then call it from the click event of both buttons.
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Call button click event from a button on anther sheet

    Thanks R.
    That solved my problem, and I also got to learn about public variables.

    Can someone tell me how to mark a thread as "Solved"? I am unable to find that button in "advanced reply" page.

    Thanks
    Ajay

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

    Re: Call button click event from a button on anther sheet

    FYI, public variables should be avoided if possible. How/why are you using them?

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

    Re: Call button click event from a button on anther sheet

    Per rule 10:
    "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 [SOLVED] 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."

  6. #6
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Call button click event from a button on anther sheet

    Hi there

    Thanks for the quick reply.

    My excel file has 2 sheets. To start, user is expected to click a custom command button on first sheet. When he does that, he is prompted for some questions. Based on his responses, I show or hide second sheet and also, set the value of a public boolean variable. All this is done in the public module.

    Then, this sub in public module calls another sub in first sheet of the file. Here, depending on the value of boolean variable, I have to execute (or not execute) certain portions of code.

    Earlier, all this code was in a single sheet. Now, I have moved some of it to a module, as suggested by you. So, since I am setting the variable value in public module, nd using this value in sheet1, I need to use public variable.

    I understand this is a bad practice as it is prone to bugs. In fact, I got one bug in my code as soon as I moved portion of my code to public module. .

    Do you think I need to change my approach?

    Thanks very much
    Ajay

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

    Re: Call button click event from a button on anther sheet

    Code in a normal module should not generally need to call code in a worksheet module (IMO) - is there a reason for the second bit of code being in a worksheet module? You can also pass the boolean variable directly between subs rather than using a public variable.

  8. #8
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Call button click event from a button on anther sheet

    1. No reason really for the second bit of code being in a worksheet module. As I said, I just transitioned from all code in a sheet to some code in a module. Maybe I should move all or max code from sheet to module.

    2.If I pass the boolean variable directly between subs rather than using a public variable, how does it reduce the chances of a bug? Is there any other advantage of not using public variable?

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

    Re: Call button click event from a button on anther sheet

    A public variable can be changed at any time by any procedure. A variable that is local to one procedure and passed directly to another cannot be affected by any other routine and hence cannot be changed unexpectedly. Public variables can also be reset by unhandled errors in your code (not that you should ever have any of those!)

  10. #10
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Call button click event from a button on anther sheet

    Makes sense. Thanks.

+ 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