How to make a Command Button on one sheet activate Command Button on other sheet?

ddub25

Well-known Member
Joined
Jan 11, 2007
Messages
625
Office Version
  1. 2019
Platform
  1. Windows
I have a Command Button on 'Sheet3 (Output)' with the following code that I was hoping would activate the Command Button on 'Sheet2 (Exercises)'. When I click it though, it comes back with a 'Run-time Error '9': Subscript out of range'. In Developer Mode, when I click on the button that I am trying to activate in 'Sheet2 (Exercises)', the code in the formula bar reads '=EMBED("Forms.CommandButton.1","")'. Any ideas why it is not working?

VBA Code:
Private Sub CommandButton1_Click()

    Sheets("Sheet2").CommandButton1_Click
    
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I would suggest that when you create a new CommandButton, that click New in the Assign Macro window. That way each button will have it's own macro and then you can do the following:
VBA Code:
Sub Button1_Click()

End Sub
Sub Sheet2_Button1_Click()
    Button1_Click
End Sub
 
Upvote 0
I would suggest that when you create a new CommandButton, that click New in the Assign Macro window. That way each button will have it's own macro and then you can do the following:
VBA Code:
Sub Button1_Click()

End Sub
Sub Sheet2_Button1_Click()
    Button1_Click
End Sub
or
VBA Code:
Sub Button1_Click()

End Sub
Sub Sheet2_Button1_Click()
    Call Button1_Click
End Sub
 
Upvote 0
I'm not sure what you mean by the Assign Macro window. Each Command Button has it's own code box/window when I right click it and click View Code. The button on the 2nd sheet just needs to contain code that will execute the Command Button on the first sheet.
 
Upvote 0
I'm not sure what you mean by the Assign Macro window. Each Command Button has it's own code box/window when I right click it and click View Code. The button on the 2nd sheet just needs to contain code that will execute the Command Button on the first sheet.
Found an answer that I think will help. Remove the Private statement from each of the CommandButton1_Click() Subs. That way they can be referenced as follows: Example shows CommandButton1_Click() macro for Sheet2 CommandButton:

VBA Code:
Sub CommandButton1_Click()
Sheet1.CommandButton1_Click
End Sub
 
Upvote 0
Solution
Thanks very much Skybot - works great. Thanks for your help, I really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top