Prevent duplicate entries using Userform, but with several listboxes

ExtraCheese

New Member
Joined
Sep 18, 2020
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
I have a userform with 3 listboxes and 1 textbox. The first listbox gives 8 unique timeslots, the second listbox a choice, and the third listbox another choice, based on the choice from listbox 2.
The code I use transforms the data into the tab 'data', which is working perfectly.
The user can pick a timeslot, then answer listbox 2 and listbox 3, then fill in the textbox (which is irrelevant for now).
What I want to prevent is that a user can pick the same choice combination per timeslot (e.g. timeslot 08:00, choice a and choice a). It is ok he can pick the same choice in a different timeslot though (e.g. timeslot 09:00, choice a and choice a).

Code:
Private Sub CommandButton1_Click()

Dim RecordRow As Long, RecordColumn As Long

RecordRow = Me.ListBox1.ListIndex + 7
RecordColumn = Me.ListBox2.ListIndex + 13

Cells(RecordRow, RecordColumn).Value = Me.TextBox1.Value

Dim irow As Long

Dim ws As Worksheet
Set ws = Worksheets("Data")
irow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

With ws
.Range("A" & irow) = ListBox1.Value
.Range("B" & irow) = ListBox2.Value
.Range("C" & irow) = ListBox3.Value
.Range("D" & irow) = TextBox1.Value
End With
ListBox1.Value = ""
ListBox2.Value = ""
ListBox3.Value = ""
TextBox1.Value = ""
End Sub
 

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.
What I want to prevent is that a user can pick the same choice combination per timeslot (e.g. timeslot 08:00, choice a and choice a).
Do you mean per time slot, ListBox2.Value can't be the same as ListBox3.Value?
Try adding the blue lines:
Rich (BB code):
Private Sub CommandButton1_Click()

Dim RecordRow As Long, RecordColumn As Long

If ListBox2.Value = ListBox3.Value Then
    Beep
    MsgBox "You can't choose ....."
    Exit Sub
End If

RecordRow = Me.ListBox1.ListIndex + 7
RecordColumn = Me.ListBox2.ListIndex + 13
.......
 
Upvote 0
Hi and thank you for your reply.
Listbox 2 has always a different value than Listbox 3.
E.g. Listbox 2 contains places, while listbox 3 contains restaurants in the selected place from listbox 2.
Users can choose timeslots from listbox 1, but should not be able to pick the same restaurant from the same timeslot. It's ok they pick the same restaurant in a different timeslot.
I hope i've made the problem clear. Thanks in advance!
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

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