Prevent Duplicate Entries in VBA

robersha

New Member
Joined
Nov 7, 2013
Messages
27
Hello,

I have a userform created that has 3 frames that contain 3 optionbuttons. Each frame contains the same optionbuttons.
Frame 1 has 3 buttons: Thursday, Friday And Saturday
Frame 2 and 3 have the same options.

What I need to do is if Frame1 option matches the selection in Frame 2 or 3, display an error message. Same goes for Frame2 matching Frame1 and 3 etc.

Below is the code I have.

Code:
If OptionButton1.Value = True And OptionButton6.Value Or OptionButton9.Value = True Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
ElseIf OptionButton2.Value = True And OptionButton5.Value Or OptionButton8.Value = True Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
ElseIf OptionButton3.Value = True And OptionButton4.Value Or OptionButton7.Value = True Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
End If

When I test this out and dont click duplicate days, I am still getting the msgbox popping up saying i have selected the same day twice. Can anyone tell me what I have wrong in this code?

Here is my full code if this helps:

Code:
Private Sub Submit_Click()



Dim First As Variant
Dim Second As Variant
Dim Third As Variant

If BadgeNumber.Text = "" Then
    MsgBox "Please Scan Your Badge", vbExclamation, "Badge Number Missing"
    Exit Sub
ElseIf OptionButton1.Value = False And OptionButton2.Value = False And OptionButton3.Value = False Then
    MsgBox "You have not selected your First Option.", vbExclamation, "Option 1 Not Selected"
    Exit Sub
ElseIf OptionButton4.Value = False And OptionButton5.Value = False And OptionButton6.Value = False Then
    MsgBox "You have not selected your Second Option.", vbExclamation, "Option 2 Not Selected"
    Exit Sub
ElseIf OptionButton7.Value = False And OptionButton8.Value = False And OptionButton9.Value = False Then
    MsgBox "You have not selected your Third Option.", vbExclamation, "Option 3 Not Selected"
    Exit Sub
End If

If OptionButton1.Value = True And OptionButton6.Value Or OptionButton9.Value = True Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
ElseIf OptionButton2.Value = True And OptionButton5.Value Or OptionButton8.Value = True Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
ElseIf OptionButton3.Value = True And OptionButton4.Value Or OptionButton7.Value = True Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
End If
RowCount = Worksheets("Sheet6").Range("A1").CurrentRegion.Rows.Count
    
    
    With Worksheets("Sheet6").Range("A1")
            .Offset(RowCount, 0).Value = Me.BadgeNumber.Value
            If OptionButton1.Value = True Then
                .Offset(RowCount, 5).Value = "Thursday"
            ElseIf OptionButton2.Value = True Then
                .Offset(RowCount, 5).Value = "Friday"
            ElseIf OptionButton3.Value = True Then
                .Offset(RowCount, 5).Value = "Saturday"
            End If
                
                If OptionButton4.Value = True Then
                    .Offset(RowCount, 6).Value = "Saturday"
                ElseIf OptionButton5.Value = True Then
                    .Offset(RowCount, 6).Value = "Friday"
                ElseIf OptionButton6.Value = True Then
                    .Offset(RowCount, 6).Value = "Thursday"
                End If
                    
                    If OptionButton7.Value = True Then
                        .Offset(RowCount, 7).Value = "Saturday"
                    ElseIf OptionButton8.Value = True Then
                        .Offset(RowCount, 7).Value = "Friday"
                    ElseIf OptionButton9.Value = True Then
                        .Offset(RowCount, 7).Value = "Thursday"
                    End If
    End With
        MsgBox "Congratulations. Your votes have been recorded."

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
this might work. Each test must be equated to a value when using the And & Or operators. Using the parenthesis to separate the Or comparison should avoid a misinterpretation by the compiler, which could give undesired results.
Code:
If OptionButton1.Value = True And (OptionButton6.Value = True Or OptionButton9.Value = True) Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
ElseIf OptionButton2.Value = True And (OptionButton5.Value = True Or OptionButton8.Value = True) Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
ElseIf OptionButton3.Value = True And (OptionButton4.Value  = True Or OptionButton7.Value = True) Then
    MsgBox "You have selected the same day twice. Please choose again.", vbExclamation, "Same Day Selected"
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,824
Members
449,127
Latest member
Cyko

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