Detect option button, exit, recheck, proceed

edge1282

New Member
Joined
Aug 14, 2018
Messages
2
Hi - new to forum. I have a userform which requires one of 5 option buttons to be selected. After pressing the command button in the user form, i want to check that an option button has been selected. if it hasn't a message should appear to warn user that they need to select a button. After clicking on OK in the message box, I want the user to be able to continue with the form and select an option button. Alternatively, I want the userform to close at that point and no further code is executed.

The code I have at present warns the user, but then continues to the end of the macro, posting incomplete info to the spreadsheet.

Start of code is as follows:

Code:
Private Sub CommandButton1_Click()
' check that an insurance company has been selected
     Dim cnt As Integer
    
    For Each ctl In Me.Controls
        If TypeName(ctl) = "OptionButton" Then
            If ctl.Value = True Then cnt = cnt + 1
            
        End If
     Next ctl
     If cnt = 0 Then MsgBox "You must select an insurance company!"

I have tried adding Exit Sub as per below. This returns me to the form, but even after selecting an option button I cannot continue.

Code:
Private Sub CommandButton1_Click()
' check that an insurance company has been selected
     Dim cnt As Integer
    
    For Each ctl In Me.Controls
        If TypeName(ctl) = "OptionButton" Then
            If ctl.Value = True Then cnt = cnt + 1
            
        End If
     Next ctl
     If cnt = 0 Then MsgBox "You must select an insurance company!"
     Exit Sub

Thanks in advance.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try
Code:
     If cnt = 0 Then
       MsgBox "You must select an insurance company!"
       Exit Sub
     End If
 
Upvote 0
Thanks Fluff. I tried that but it gave an error. I've added in some workaround code that allows the incomplete data to be added to the table but then looks for the incomplete row and deletes it. This seems to be working fine.
 
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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