Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
To resolve this behavior, use one of the following three methods.
Method 1: Changing the Order of the Lines of Code
In the Visual Basic macro or procedure that displays the
UserForm, set the focus to another control before setting the
Value property of the
OptionButton control to
True. The following example demonstrates this method:
- Open a new workbook in Microsoft Excel.
- On the Tools menu, point to Macro, and then click Visual Basic Editor.
- On the Insert menu, click UserForm.
- If the Toolbox is not displayed, click Toolbox on the View menu.
- Drag an OptionButton, a RefEdit, a TextBox, and a CommandButton control to the UserForm from the Toolbox.
- Double-click the CommandButton control and type the following code:
Private Sub CommandButton1_Click()
UserForm1.Hide
End
End Sub
- Close the code window.
- Double-click the OptionButton control and type the following code:
Private Sub OptionButton1_Click()
UserForm1.RefEdit1.SetFocus
End Sub
- Close the code window.
- On the Insert menu, click Module.
- Type the following into the new module:
Sub RunForm()
With UserForm1
.TextBox1.SetFocus
.OptionButton1.Value = True
.Show
End With
End Sub
- On the Tools menu, point to Macro, and then click Macros. Click RunForm, and then click Run.
The macro runs and sets the focus correctly.
Method 2: Do Not Set Focus with Code Attached to an OptionButton Control
Do not assign code to an
OptionButton control to set the focus to a
RefEdit control before a form is displayed. Instead, leave any
OptionButton controls not selected to allow the user to make the choice.
Method 3: Do Not Set the Value Property of an OptionButton Control to True
Do not set the focus to another control in the procedure or macro that
displays the
UserForm if the same procedure or macro sets the
Value property of the
OptionButton control to
True. Setting the
Value property of the
OptionButton control to
True triggers the Click event procedure that is assigned to the
OptionButton control.