how would I code this? thank you
how would I code this? thank you
![]()
Sub Start_Form() UserForm1.Show End Sub Private Sub CommandButton1_Click() MsgBox ("This code will be run") End Sub Private Sub UserForm_Initialize() Me.OptionButton1.Value = True CommandButton1_Click End Sub
thanks for the reply! but the form can also me opened manually so I can have it automatically click that button.. is the a way to have this done without having it do this when the form Initializes?
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:
Ordinary 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
------------------------![]()
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:
Ordinary Module code:![]()
Private Sub CommandButton1_Click() MsgBox "CommandButton1_Click() activated on " & Now & "." End Sub
Lewis![]()
Sub DisplayUserForm2NormalRuntimeUsage() UserForm2.OptionButton1.Value = True UserForm2.CommandButton1 = True UserForm2.Show End Sub
thank you!! that's great!!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks