View Full Version : [SOLVED:] Move focus to CommandButton
mdmackillop
07-14-2005, 02:02 PM
Arising from a question recently:
How can I move the focus to a Control Toolbox CommandButton using a SelectionChange event. It doesn't appear to have a SetFocus property. Activate runs the code behind the button, which I may not wish to do.
Jacob Hilderbrand
07-14-2005, 02:17 PM
You can select it.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.CommandButton1.Select
End Sub
mdmackillop
07-14-2005, 02:23 PM
Hi Jake,
This does not select it in a manner that activates it when you press Enter. Or is this not an available option?
Jacob Hilderbrand
07-14-2005, 02:28 PM
Well, how about something like this.
Option Explicit
Dim Flag As Boolean
Private Sub CommandButton1_Click()
If Flag = True Then
Exit Sub
End If
MsgBox "Running Macro..."
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Flag = True
Me.CommandButton1.Activate
Flag = False
End Sub
If the Selection Change event activates the Command Button, the code will not run.
mdmackillop
07-14-2005, 02:37 PM
Thanks Jake,
That does the selection, but unfortunately Enter does not trigger the button, which is what I was after. I'll file away the flag setting though. Potentially very useful.
Jacob Hilderbrand
07-14-2005, 03:45 PM
Space Bar will trigger the button.
mdmackillop
07-14-2005, 11:19 PM
:doh: footinmout :blush
KB Item?
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.