Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel: a button clicking other buttons

Status
Not open for further replies.

yoyoyojohnny

Technical User
Nov 3, 2003
9
CA
Hi,
I have a problem with my macro writing in Excel:
I have 4 worksheets with one button on each of them. When I want to perform something, I have to click each of these buttons. That means I have to: go to sheet1 and click the button; go to sheet2 and click the button; go to sheet3 and click the button; go to sheet4 and click the button.
Intead of that, I want to put one additional button on each worksheet that, once clicked, would trigger the click event for all the four existing buttons. So that, no matter what sheet I am looking at, when I click the "general" button, I will have the program execute the code for all 4 existing buttons.
I know that I am doubling the work here: a button that would click another button. You could ask: why not to write the code for each already existing button - the code that would handle all the tasks in each worksheet, but the reason is that I basically gave the same name to all the existing buttons and the variables have the same name etc. So it wil be a pain in the neck to redefine all this (the code is long). I would like it the way it is with one more button on each worksheet that, as I described, would push me the existing buttons without me going to each sheet and doing it myself.

I tried to activate the worksheet and the buttons on those worksheets, but it did not work, maybe I was doing somehting wrong... Please help me with this issue.

Hope my question is clear.
 
I thought about it, but that means that the button on the first sheet will have the code from the buttons on the other 3 sheets (2, 3 and 4); the button on the second sheet will have the code from the other 3 sheets (1, 3 and 4) etc.

I know that when I have the 4 buttons on the same sheet I just write:

Private Sub cmdSortAll_Click()
Call cmdSortBut1_Click
Call cmdSortBut2_Click
Call cmdSortBut3_Click
Call cmdSortBut4_Click
End Sub


And I would like to have something as simple as this in my code with the 4 sheets.
 
For each of the four current buttons: Take the "guts" out of the pushbutton sub and put them into four subs in a module. You may need to put a Worksheets("xxx").Activate at the top of the code for each sub. Then call the appropriate sub from each pushbutton.

After the restructuring and when you have it working again the way it does now, create a fifth sub that does nothing but call each of the other four subs:

Sub DoAll()
SortA
SortB
SortC
SortD
End Sub

Then you can put a button on each sheet with code on the click event to execute the DoAll sub.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top