View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Harald Staff Harald Staff is offline
external usenet poster
 
Posts: 1,327
Default Set focus on command button

Hi Derek

ActiveSheet.CommandButton1.Activate
seem to do what you want. SetFocus is used on forms with a Tab order; you
can Tab between controls. Controls on a worksheet doesn't behave that way,
so I guess that might cause the problem.

It does btw not click on Enter. Space bar is the usual keypress for it. If
you need Enter you'd have to code it (at least in Excel 2000) like this :

Private Sub CommandButton1_Click()
MsgBox "Clicked !"
End Sub

Private Sub CommandButton1_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then Call CommandButton1_Click
End Sub

--
HTH. Best wishes Harald
Followup to newsgroup only please

"Derek Gadd" skrev i melding
om...
I have a command button on my worksheet and the code
"CommandButton1.SetFocus" gives the error message "Object doesn't
support this property or method". But the help shows SetFocus as one
of the methods for CommandButton. Why doesn't it work? I want to set
the focus so that the user can just press enter to click the command
button. Is this the correct approach?

TIA,
Derek