Framed OptionButtons - Error if Blank

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I am using Exel 2007 and have a Userform for data entry. There are 8 different frames with each frame containing 5 option buttons. I would like to prevent the user from "adding" the item unless they have choosen one of the 5 option buttons in each frame, so they would get an error message saying they must choose the options before proceeding.

I have 8 different AddData routines for each frame, Frame1, Frame2, etc. How do I tell it to check the option buttons within each Frame to make sure one has been choosen? Here is an example of the code for one of the AddData rountines if that helps:

Private Sub AddData1()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Data")
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
ws.Cells(iRow, 1).Value = Me.txt_date.Value
ws.Cells(iRow, 2).Value = "Planer"
ws.Cells(iRow, 3).Value = "Breakdown Hoist Area"
ws.Cells(iRow, 4).Value = Me.cbo_shift.Value
ws.Cells(iRow, 5).Value = "Infeed Area"
ws.Cells(iRow, 7).Value = Me.txt_minutes1.Value
ws.Cells(iRow, 8).Value = Me.txt_NoteInfeedArea.Value
ws.Cells(iRow, 9).Value = Me.cbo_employee.Value

If opt_mech1.Value = True Then ws.Cells(iRow, 6).Value = "Mechanical"
If opt_elec1.Value = True Then ws.Cells(iRow, 6).Value = "Electrical"
If opt_oper1.Value = True Then ws.Cells(iRow, 6).Value = "Operator"
If opt_hu1.Value = True Then ws.Cells(iRow, 6).Value = "Hang Up"
If opt_other1.Value = True Then ws.Cells(iRow, 6).Value = "Other"

End Sub

Thanks in Advance for your help,
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Change name of Frame1, etc. to meet your needs:

Code:
Dim OptBtnControl As Control
Dim boolFrame1 As Boolean
Dim boolFrame2 As Boolean
Dim boolFrame3 As Boolean
 
boolFrame1 = False
For Each OptBtnControl In UserForm1.Frame1.Controls
    If TypeName(OptBtnControl) = "OptionButton" Then
        If OptBtnControl.Value = True Then
            boolFrame1 = True
        End If
    End If
Next OptBtnControl
If boolFrame1 = False Then
    MsgBox "Please pick an option in Frame1."
End If
 
boolFrame2 = False
For Each OptBtnControl In UserForm1.Frame2.Controls
    If TypeName(OptBtnControl) = "OptionButton" Then
        If OptBtnControl.Value = True Then
            boolFrame2 = True
        End If
    End If
Next OptBtnControl
If boolFrame2 = False Then
    MsgBox "Please pick an option in Frame2."
End If
 
boolFrame3 = False
For Each OptBtnControl In UserForm1.Frame3.Controls
    If TypeName(OptBtnControl) = "OptionButton" Then
        If OptBtnControl.Value = True Then
            boolFrame3 = True
        End If
    End If
Next OptBtnControl
If boolFrame3 = False Then
    MsgBox "Please pick an option in Frame3."
End If
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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