How can I reset the default button control on a userform?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,491
Office Version
  1. 365
Platform
  1. Windows
I have a simple userform with 2 buttons: Win, Lose, & Done. I set the default property for the Win button to True. When the form first comes up, Win is selected. But if I click on Lose, when the form comes back up, Lose is selected. How can I make the Win button always the default?

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
because you are hiding your userform and not unloading it, the startup settings are not being loaded. this will restore default to comandbutton1 when the form reappears
VBA Code:
Private Sub UserForm_Activate()
    CommandButton1.Default = True
End Sub
 
Upvote 0
because you are hiding your userform and not unloading it, the startup settings are not being loaded. this will restore default to comandbutton1 when the form reappears
VBA Code:
Private Sub UserForm_Activate()
    CommandButton1.Default = True
End Sub
I don't know what you mean by "hiding". The name of the form is "Score". In Module1, I have this code:
VBA Code:
Sub Score()
'Display the Score form
uScore.Show
End Sub

When I first load the sheet, there is a Score button. If I click on it, I get the userform with the 3 buttons. If I click on wither Win or Lose, it scores that result and the form stays up.

Where to I put your code? In Module1?
 
Upvote 0
if uScore is always visible then you will need to insert CommandButton1.Default = True into the code for the "lose button" you did the other day
 
Upvote 0
if uScore is always visible then you will need to insert CommandButton1.Default = True into the code for the "lose button" you did the other day
Here's the code in Module1:
VBA Code:
Sub Score()
'Display the Score form
uScore.Show
End Sub

Private Sub UserForm_Activate()
    ButtonWin.Default = True
End Sub

And here's the start and end of the Lose button code:
Code:
Private Sub ButtonLoss_Click()
Const title As String = "Loss Button"

Call LoadVars

   . . .

End Select
    
MsgBox msg1 & vbCrLf & vbCrLf & msg2, vbOKOnly, title
ButtonWin.Default = True

End Sub

The Lose button still remains selected after it is used.
 
Upvote 0
try this
VBA Code:
    MsgBox msg1 & vbCrLf & vbCrLf & msg2, vbOKOnly, Title
    ButtonWin.Default = True
    DoEvents
End Sub
 
Upvote 0
Default does not affect which control has the focus. If you want to set focus to a different control, you should use controlname.SetFocus in the Activate event.
 
Upvote 0
Solution
Default does not affect which control has the focus. If you want to set focus to a different control, you should use controlname.SetFocus in the Activate event.
That did it. Thanks, Rory. (y) :)
 
Upvote 0

Forum statistics

Threads
1,213,562
Messages
6,114,326
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top