ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   click with code (https://www.excelbanter.com/excel-programming/306531-click-code.html)

libby

click with code
 
Hi,

Is there any way to click a button on a worksheet using
code.
So far I've assigned an accelerator to the button which
works if I actually do it, but not when I use SendKeys to
do it.

Here is my code. I've simplified it to highlight the
problem.
It's a sheet with 2 commandbuttons and code in the
BeforeClose event.

Private Sub Commandbutton1_Click()
if Msgbox("OK to close?", vbyesno)=vbyes then 'click yes
ThisWorkBook.Close
end if
end sub

Private Sub CommandButton2_Click()'set an accelerator
of "A"
Workbooks.Open("another workbook")
End sub

Private Sub Workbook_Beforeclose(Cancel as Boolean)
if msgbox("Do you want to open the other
workbook?",vbyesno)= vbyes then 'click yes
cancel = true
sendkeys "%A"
else
thisworkbook.saved= true
end if
exit sub




Fank Kabel

click with code
 
Hi
why not call directly the code behind this button in your
events. You may put the processing code in a public sub
and call this from your other events?

-----Original Message-----
Hi,

Is there any way to click a button on a worksheet using
code.
So far I've assigned an accelerator to the button which
works if I actually do it, but not when I use SendKeys to
do it.

Here is my code. I've simplified it to highlight the
problem.
It's a sheet with 2 commandbuttons and code in the
BeforeClose event.

Private Sub Commandbutton1_Click()
if Msgbox("OK to close?", vbyesno)=vbyes then 'click yes
ThisWorkBook.Close
end if
end sub

Private Sub CommandButton2_Click()'set an accelerator
of "A"
Workbooks.Open("another workbook")
End sub

Private Sub Workbook_Beforeclose(Cancel as Boolean)
if msgbox("Do you want to open the other
workbook?",vbyesno)= vbyes then 'click yes
cancel = true
sendkeys "%A"
else
thisworkbook.saved= true
end if
exit sub



.



All times are GMT +1. The time now is 05:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com