Retroshift
Board Regular
- Joined
- Sep 20, 2016
- Messages
- 119
- Office Version
- 2019
- Platform
- Windows
Hi, I have a data list and a userform to add additional row(s) with data. I would like an error message to pop up whenever duplicate data is entered in the according userform field on the basis of a specific column in a named table. A blank entry field is admitted, but no duplicate entries. I have a sub with code below but the code always triggers an error message saying that the entered value is a duplicate, even when it is not a duplicate. How can the code be altered so to only trigger an error message with real duplicate entries?
VBA Code:
Private Sub txtShortName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Set wsh = ThisWorkbook.Worksheets("List")
Set tbl = wsh.ListObjects("tblCls")
shortname = txtShortName.Value
If shortname = "" Then Exit Sub
If Not Application.WorksheetFunction.CountIf(tbl.DataBodyRange.Columns(3), shortname) > 1 And Len(shortname) > 0 Then
txtShortName.Value = ""
txtShortName.SetFocus
Cancel = True
MsgBox "The short name [" & shortname & "] already exists.", vbCritical, "DUPLICATE ENTRY"
End If
End Sub