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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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