Hi Jenkins27,
See the attached file and the code below which is included in the file. Please note that OptionButtons are usually placed in a Group, to allow more than one set of OptionButtons on a UserForm.
There is more than one way to accomplish what you want.
Please note that in both cases the CommandButton Event Handler gets activated before the UserForm is displayed.
There are two ways to display a UserForm, Modal (which locks out all Excel Resources) and nonModal or Modeless (which allows Excel resources to be accessed).
When a UserForm is loaded as Modal, macro execution is paused at the line that loads the UserForm. Execution continues when the '.Hide' or the 'Unload' command is executed. That is why the UserForm commands are before the .Show command in Sub DisplayUserForm2NormalRuntimeUsage().
When a UserForm is loaded as NonModal, macro execution continues after the .Show command.
The commands for each are displayed here:
UserForm1.Show 'Load a UserForm in Modal mode
'or
UserForm1.Show vbModal 'Load a UserForm in Modal mode
UserForm1.Show False 'Load a UserForm in Non Modal mode
'or
UserForm1.Show vbModeless 'Load a UserForm in Non Modal mode
------------------------
In the first case (UserForm1), the heavy lifting is done by UserForm_Initialize() which is automatically called when the UsefForm is loaded (.show) command.
UserForm1 module code:
Private Sub UserForm_Initialize()
OptionButton1.Value = True
CommandButton1_Click
End Sub
Private Sub CommandButton1_Click()
MsgBox "CommandButton1_Click() activated on " & Now & "."
End Sub
Ordinary Module code:
Sub DisplayUserForm1NormalRuntimeUsage()
UserForm1.Show
End Sub
------------------------
In the second case (UserForm2), the ordinary module code sets the values before the UserForm is loaded.
UserForm2 module code:
Private Sub CommandButton1_Click()
MsgBox "CommandButton1_Click() activated on " & Now & "."
End Sub
Ordinary Module code:
Sub DisplayUserForm2NormalRuntimeUsage()
UserForm2.OptionButton1.Value = True
UserForm2.CommandButton1 = True
UserForm2.Show
End Sub
Lewis
Bookmarks