Finding duplicated values in a cell with userform

Wicked_

Board Regular
Joined
Jun 5, 2018
Messages
81
Hi.

I have a userform, with two textboxes, and a button.
The button enters the values (text and numbers) from the textbox to cells.
Example: Textbox1 and textbox 2. If i write 1A in txbox 1 and 2A in txbox 2, then it enters that value in A2, and B2, then if i type new values, it goes in A3 and B3 and so on.

But is there a way to find duplicates as i press the button to insert the values to the cell?

Example.

If A2 and B2 is Test1 and Test2, then if i enter Test1 and Test2 again, i want a msgbox to "prevent" the user from inserting it, because it already exists in the the sheet.

Is there a easy way to do this?

Thanks :)
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi.
Here you go.

What i forgot to mention, is i need to check only the A colomn only. B colomn can have the same entry, but not the A column :)


Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")


'find first empty row in database
'iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
' SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
iRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1


'check for a Name number
If Trim(Me.IDone.Value) = "" And Me.IDTwo.Value = "" Then
Me.IDOne.SetFocus
MsgBox "Please complete the form"
Exit Sub
End If




If Me.IDone.Value = "" Or Me.IDTwo.Value = "" Then GoTo Add Else GoTo Go
Add:
If Me.IDone.Value = "" Or Me.IDTwo.Value = "" Then
MsgBox "Please complete the form"
Else


Go:


ws.Cells(iRow, 1).Value = Me.IDOne.Value
ws.Cells(iRow, 2).Value = Me.IDTwo.Value




End If






MsgBox "Data submitted!", vbOKOnly + vbInformation, "Data Added"
End If


Me.IDOne.SetFocus
 
Upvote 0
Try this:
Code:
Private Sub CommandButton1_Click()
'Modified  6/13/2019  4:30:57 AM  EDT
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Dim Lastrowa As Long
Lastrowa = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
Dim Lastrowb As Long
Lastrowb = ws.Cells(Rows.Count, "B").End(xlUp).Row + 1
Dim ans As Long
Dim r As Variant
r = IDOne.Value
If IDOne.Value = "" Or IDTwo.Value = "" Then MsgBox "Please complete the form": IDOne.SetFocus: Exit Sub
ans = Application.WorksheetFunction.CountIfs(ws.Range("A1:A" & Lastrowa - 1), r)
If ans > 0 Then MsgBox r & "  Already exist in column A" & vbNewLine & "I will stop this script": Exit Sub
ws.Cells(Lastrowa, "A").Value = IDOne.Value
ws.Cells(Lastrowb, "B").Value = IDTwo.Value
MsgBox "Data submitted!", vbOKOnly + vbInformation, "Data Added"
IDOne.SetFocus
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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