![]() |
Unable to call Command Button's Click() method from a different sheet
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 |
Unable to call Command Button's Click() method from a different sh
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. -- HTH... Jim Thomlinson " wrote: 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 |
Unable to call Command Button's Click() method from a differen
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 "Jim Thomlinson" wrote: 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. -- HTH... Jim Thomlinson " wrote: 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 |
All times are GMT +1. The time now is 01:37 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com