Home » excel » excel – Command Button that runs (click) on another button in another sheet

excel – Command Button that runs (click) on another button in another sheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

Sheet 4(Name 1) has a button, this button will take the user to another Sheet with a graph Sheet3(Graph)

Private Sub CommandButton1_Click()

ThisWorkbook.Sheets("Graph").Activate
End Sub

Goal: Have the button in Sheet 4(Name 1) to also click on another button in another sheet1(Basic) this will result with updating the graph.

I understand that it may sound easier to just have the button to also excute the code but not in this case.

How to&Answers:

The “other button” probably looks something like this:

Private Sub CommandButton42_Click()
    'do stuff...
End Sub

Change it to this:

Private Sub CommandButton42_Click()
    DoStuff
End Sub

Put the DoStuff procedure in a standard module (note the Public access modifier):

Public Sub DoStuff()
    'do stuff...
End Sub

If you don’t want DoStuff to be visibly exposed as a macro, add Option Private Module at the top, near where you put Option Explicit 🙂

And then your button can do this:

Private Sub CommandButton1_Click()
    DoStuff
End Sub

Don’t invoke Click handlers from user code – event handlers are meant to be Private and invoked by event provider objects.