Set focus on command button

  • Thread starter Thread starter Derek Gadd
  • Start date Start date
D

Derek Gadd

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
 
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
 
Derek,

Worksheet commandbuttons don't understand the SetFocus method, so you can't
do that. You can, however, set the focus to the command button like so

ActiveSheet.CommandButton1.Activate

but this doesn't help you as it also doesn't have a Default (or Cancel)
property, so you cannot set it to respond to Enter (or Esc).

Sorry.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Back
Top