Hello
I am new to VBA but have had some success creating a user form that enters data into a table on clicking save. What I am struggling with is checking for duplicates and adding a pop up when there are duplicates. I have found some good info on how to do this but not on how to check multiple cells for each record, as I have first name and surname in adjcacent columns and need to check both. I envisage the process to be;
On user clicking save;
Check columns A and B for duplicates within table
If BOTH are duplicated, display message box "this user already exists, do you still wish to save"
If clicks yes, saves record as normal
If no, clears form
This is my existing code for the form when save is clicked (I need to check both Name1TEXT and Name2TEXT for duplicates and only return message box if both are duplicated within the same record. In other words, i'm checking for the full name already existing in my table.
'Save new user info in New User Table
Private Sub Save_Click()
Dim LastRow As Range
Dim UserTable As ListObject
'Add row to bottom of New User Table
Sheet7.ListObjects("NewUserTable").ListRows.Add
'Enter data from form into new row at bottom
Set UserTable = Sheet7.ListObjects("NewUserTable")
Set LastRow = UserTable.ListRows(UserTable.ListRows.Count).Range
With LastRow
.Cells(1, 1) = Name1TEXT.Value
.Cells(1, 2) = Name2TEXT.Value
.Cells(1, 3) = GenderCOMBO.Value
.Cells(1, 4) = PostcodeTEXT.Value
.Cells(1, 8) = Contact1TEXT.Value
.Cells(1, 9) = Contact2TEXT.Value
.Cells(1, 10) = DateRegTEXT.Value
.Cells(1, 11) = GroupCOMBO.Value
.Cells(1, 13) = EmpCOMBO.Value
.Cells(1, 19) = MedicalDetailsTEXT.Value
.Cells(1, 20) = EthnicityCOMBO.Value
.Cells(1, 21) = ReligionCOMBO
.Cells(1, 22) = SexualityCOMBO
.Cells(1, 24) = AgeCOMBO
'result for needs 1 checkbox
If PhysDysCHECK.Value = True Then
.Cells(1, 14) = "Physical Disability"
Else
.Cells(1, 14) = "None"
End If
'result for needs 2 checkbox
If LearnDiffCHECK.Value = True Then
.Cells(1, 15) = "Learning Difficulty"
Else
.Cells(1, 15) = "None"
End If
'result for needs 3 checkbox
If MentalHCHECK.Value = True Then
.Cells(1, 16) = "Mental Health"
Else
.Cells(1, 16) = "None"
End If
'result for needs 4 checkbox
If PhysHCHECK.Value = True Then
.Cells(1, 17) = "Physical Health"
Else
.Cells(1, 17) = "None"
End If
'result for allergies checkbox
If AllergiesCHECK.Value = True Then
.Cells(1, 18) = "Yes"
Else
.Cells(1, 18) = "No"
End If
'result for GDPR checkbox
If GDPR.Value = True Then
.Cells(1, 25) = "Yes"
Else
.Cells(1, 25) = "No"
End If
End With
End Sub
Any help would be very much appreciated
Bookmarks