Prompt for unselected option button

TerenceTitus

New Member
Joined
Feb 8, 2017
Messages
20
Hi,

I am facing some trouble formulating a macro to run upon closure of workbook. I would like to run a macro to check if there are unselected option buttons within my specific sheet. The option buttons grouped in rows of 5 where only 1 can be selected per row and it runs down as long as the survey continues.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

For Each vctrl In ActiveWorkbook.Worksheets("Survey").Shapes
    If vctrl.Type = msoFormControl Then
        If vctrl.FormControlType = xlOptionButton Then
            If vctrl.DrawingObject.Value = False Then MsgBox Prompt:="You have not answered all questions!"
                If MsgBox("Do you still want to close the survey?", vbYesNo) = vbNo Then Exit Sub
        End If
    End If
    Next
End Sub

It does not prompt me on unselected option buttons and I cant make the macro end if the user selects do not close survey.
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this and see if it works for you :
Code:
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim vctrl As Shape, iFramesCount As Integer, iSelectdOptionButtonsCount As Integer
    
    For Each vctrl In ActiveWorkbook.Worksheets("Survey").Shapes
        If vctrl.Type = msoFormControl Then
            If vctrl.FormControlType = xlGroupBox Then
                iFramesCount = iFramesCount + 1
            End If
        End If
    
        If vctrl.Type = msoFormControl Then
            If vctrl.FormControlType = xlOptionButton Then
                If vctrl.OLEFormat.Object.Value = 1 Then
                    iSelectdOptionButtonsCount = iSelectdOptionButtonsCount + 1
                End If
            End If
        End If
    Next
    
    If iFramesCount > iSelectdOptionButtonsCount Then
        MsgBox Prompt:="You have not answered all questions!"
        If MsgBox("Do you still want to close the survey?", vbYesNo) = vbNo Then Cancel = True
    End If

End Sub
 
Last edited:
Upvote 0
Dear Jaafar,

Many thanks for your help, it works! I didnt know how to use the frame as indicated in your code, i only knew that the command had to recognise the grouping somehow.

Sorry to ask again, i have another problem that i linked to the one you just solved. I have a command button ("Clear Data") for users to remove their responses (unselect the grouped option buttons). It worked in my previous version when i didnt group the buttons by rows, they were as individuals. I tried combining what you provided me about the groupbox but im still not successful.

Code:
Private Sub Clear_Click()

'message bos to confirm deletion
If MsgBox("This will delete your responses! Are you sure?", vbYesNo) = vbNo Then Exit Sub


Dim vctrl As Shape, iFramesCount As Integer, iSelectdOptionButtonsCount As Integer
    
    For Each vctrl In ActiveWorkbook.Worksheets("Survey").Shapes
        If vctrl.Type = msoFormControl Then
            If vctrl.FormControlType = xlGroupBox Then
                iFramesCount = iFramesCount + 1
            End If
        End If
    
        If vctrl.Type = msoFormControl Then
            If vctrl.FormControlType = xlOptionButton Then
                vctrl.OLEFormat.Object.Value = False
            End If
        End If
    Next
    
    'create a message box
MsgBox Prompt:="Your responses has been deleted"


End Sub
 
Upvote 0
Upu could put this Macro in the ThisWorkbook Module and assign it to your ("Clear Data") Button

Code:
Public Sub ClearOptionButtons()

    Dim vctrl As Shape

    For Each vctrl In ActiveWorkbook.Worksheets("Survey").Shapes
        If vctrl.Type = msoFormControl Then
            If vctrl.FormControlType = xlOptionButton Then
            vctrl.OLEFormat.Object.Value = 0
            End If
        End If
    Next
End Sub
 
Upvote 0
Are you trying to clear all of the Form Control OptionButtons on the worksheet (no matter what Frame they are in)? If so, this single line of code should work...

Sheets("Survey").OptionsButtons = False
 
Upvote 0
Hi Both Jaafar and Rick,

Sorry for the late reply. I have tried both ways that you suggested, sadly it did not work. It works for option boxes that are not grouped together. I'm not too sure as to why it does not work.

I am currently using a group box form control for each row, 5 options boxes each.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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