error if option button in frame is not selected in userform reopen userform

AwesomeSteph

Board Regular
Joined
Aug 18, 2017
Messages
80
I have a userform that I would like to reopen after an error pops up if option buttons aren't selected within frames.

I have a code that works for the error but once the error is closed the userform is also closed. I need the user to be forced to make a selection from both frames in the userform.

Code:
Private Sub CommandButton1_Click()
    Unload Me
        
If OptionButton1.Value = True Then
    Sheet8.CheckBox1.Value = False
    Sheet7.Visible = True
End If
If OptionButton2.Value = True Then
    Sheet8.CheckBox1.Value = True
    Sheet7.Visible = False
End If
If OptionButton3.Value = True Then
    Sheet8.CheckBox2.Value = False
    Sheet7.Visible = True
End If
If OptionButton4.Value = True Then
    Sheet8.CheckBox2.Value = True
    Sheet7.Visible = False
End If


For Each ctrl In Frame1.Controls


   If TypeOf ctrl Is msforms.OptionButton Then


       If ctrl.Value = True Then
             F1D = True
             Exit For
             End If
       End If
   Next ctrl


If F1D = False Then
MsgBox "No Option Selected In Frame 1"
End If


For Each ctrl In Frame2.Controls


   If TypeOf ctrl Is msforms.OptionButton Then


       If ctrl.Value = True Then
             F2D = True
             Exit For
             End If
       End If
   Next ctrl


If F2D = False Then
MsgBox "No Option Selected In Frame 2"
End If


End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
First thing to do is remove this from the top of the code.
Code:
    Unload Me

This will unload the userform after the rest of the code has run regardless of what happens in that code.

You should probably move it to the end of the code and make sure it only gets executed when you are sure the requirements have been met, e.g. all the appropriate selections made.
 
Upvote 0
Setting each group of option buttons to a default value at start up will stop any problems caused by unselected groups of buttons.
 
Upvote 0
You can't just move it.

You either need to move it and add conditions to make sure it doesn't get executed unless all the requirements are met or add to the preceding code so that when a condition isn't met the sub is exited.

The latter might look something like this.
Code:
If F1D = False Then
    MsgBox "No Option Selected In Frame 1"
    Exit Sub ' and do not dismiss the userform
End If
 
Upvote 0
Setting each group of option buttons to a default value at start up will stop any problems caused by unselected groups of buttons.

I'm not 100% sure if I understand what you mean. If you mean having one option button from each frame pre-selected this is not an option I have users that will ignore it and have the wrong options selected on other forms because of this.
 
Upvote 0
You can't just move it.

You either need to move it and add conditions to make sure it doesn't get executed unless all the requirements are met or add to the preceding code so that when a condition isn't met the sub is exited.

The latter might look something like this.
Code:
If F1D = False Then
    MsgBox "No Option Selected In Frame 1"
    Exit Sub ' and do not dismiss the userform
End If

That was perfect! Thank you!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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