
Clicking any of the CommandButtons (except the OKButton) executes the ButtonGroup_Click procedure.

If ctl.Name "OKButton" Then 'Skip the OKButton ButtonCount = ButtonCount + 1 ReDim Preserve Buttons(1 To ButtonCount) Set Buttons(ButtonCount).ButtonGroup = ctl End If End If Next ctl End SubĪfter performing these steps, you can execute the ShowDialog procedure to display the UserForm. ' Create the Button objects ButtonCount = 0 Listing 15-4: Establishing the Buttons() Object Arrayĭim ButtonCount As Integer Dim ctl As Control Therefore, clicking the OKButton does not execute the ButtonGroup_ Click procedure.
#SCRIPTCASE FORM OPEN WITH ONLY OK BUTTON CODE#
Notice that the code excludes a button named OKButton from the button group. This procedure is kicked off by the UserForm's Initialize event.

In the code module for the UserForm, enter the code in Listing 15-4. This routine simply displays the UserForm:Ĥ. Insert a normal VBA module and enter the following code. Msg = Msg & "Top Position: " & ButtonGroup.Top MsgBox Msg, vblnformation, ButtonGroup.Name End Subģ. Msg = Msg & "Left Position: " & ButtonGroup.Left _ & vbCrLf Msg = Msg & "Caption: " & ButtonGroup.Caption _ & vbCrLf Msg = "You clicked " & ButtonGroup.Name & vbCrLf _ & vbCrLf Public WithEvents ButtonGroup As MsForms.CommandButton You will need to customize the ButtonGroup_Click procedure. Insert a class module into your project (choose Insert ^ Class Module), give it the name BtnClass, and enter the following code. (The example on the CD contains 16 CommandButttons.) This example assumes that the form is named UserForm1.Ģ. Create your UserForm as usual and add several CommandButtons.
#SCRIPTCASE FORM OPEN WITH ONLY OK BUTTON HOW TO#
The following steps describe how to re-create the example workbook:ġ. This example is available on the companion CD-ROM. This section describes a way around this limitation by using a class module to define a new class. You might prefer to have a single procedure that can determine which button was clicked and take the appropriate action. If your UserForm has many CommandButtons, setting up all these event handlers can get tedious. In the following examples, clicking either CommandButtonl or CommandButton2 both execute the ButtonClick procedure, and the single argument tells the ButtonClick procedure which button was clicked. You can, however, have each event handler call another all-inclusive macro in the event handler procedures, but you'll need to pass an argument to indicate which button was clicked.

Each Click event handler is hard-wired to its CommandButton. In other words, you cannot assign a macro to execute when any CommandButton is clicked. Private Sub CommandButton2_Click() ' Code goes here End Sub Private Sub CommandButtonl_Click() ' Code goes here End Sub For example, if you have two CommandButtons, you'll need at least two event handler procedures: Every CommandButton on a UserForm must have its own procedure to handle its Click event.
