Unable to call Command Button's Click() method from a different sheet

Z

Zachary_Harrison

Hi all-

I have a command button on Sheet1, with a Command1_Click() method. I
want to call that procedure from the Command2_Click() method which is
on Sheet2. Even with both procedures defined as Public, I am unable
to get this to work. I have tried

Call Command1_Click()
Call Command1.click

I get "Sub or function not defined" for Command1_Click() & "Object
Required" for Command1.click.

Any thoughts?

Thank you in advance!

Zacah
 
G

Guest

You need to fully declare where the command button lives otherwise how would
the program know where to look. You could have the same command button on 2
or more sheets... Give this a try.

Call Sheet1.Command1_Click()
or
Call Sheets("My Sheet").Command1_Click()

You are correct that the procedure needs to be public so leave that as it is.
 
G

Guest

Zach,

While this will work just fine, my preference is to not call one event
handler from another. Instead I would create a class or a module, put the
common code in a helper method that gets called from both event handlers.
The reason for this is that if for some reason you change or remove Button1
then Button2 will be ignorant since it's now decoupled from Button1.

for Example:

Sub Button1_Click()
Call HelperMethod()
End Sub

Sub Button2_Click()
Call HelperMethod()
End Sub

Sub HelperMethod()
' Put your common code here.
End Sub

- Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top