Private Sub CommandButton1_Click()
AccessFormSearch.Hide
Dim sNewName As String
Dim lPosition As Long
Dim rEmpList As Range
Dim answer As String
Dim confirm As String
Dim i As Integer
Dim intValueToFind As String
Set rEmpList = Sheets("Index").Range("Name") 'adjust as required
Ask:
sNewName = TextBox1.Value 'InputBox("Enter the name of an employee")
If sNewName = "" Then Exit Sub 'GoTo Out
If sNewName <> "" Then GoTo Check
Check:
intValueToFind = sNewName
For i = 1 To 1500 ' Revise the 1500 to include all of your values
If Cells(i, 1).Value = intValueToFind Then
MsgBox ("This name already exists on row " & i)
ActiveWindow.ScrollRow = i
lPosition = Application.WorksheetFunction.Match(sNewName, rEmpList, 1)
Range("A" & lPosition + 1).Select
Unload Me
Exit Sub
End If
Next i
PartialStringCheck:
intValueToFind = sNewName
Dim lngLastRow As Long
Dim lngRow As Long
Dim strValue As String
Dim lngRowOutput As Long
' where does the data end
lngLastRow = Sheets("Index").Range("Name").Rows.Count
If lngLastRow = 1 Then Exit Sub ' no data
lngRowOutput = 2
For lngRow = 2 To lngLastRow
strValue = Sheets("Index").Cells(lngRow, 1).Value
If strValue = vbNullString Then
GoTo Notfound
Else
If InStr(1, strValue, intValueToFind, vbTextCompare) > 0 Then
MsgBox ("This name may already exist on row " & lngRow)
ActiveWindow.ScrollRow = lngRow
Range("A" & lngRow).Select
Unload Me
Exit Sub
End If
End If
Next lngRow
Exit Sub
Notfound:
AccessFormSearchNotFound.Show
End Sub
Private Sub CommandButton2_Click()
AccessFormSearch.Hide
Unload Me
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
CommandButton1_Click
End Sub
Private Sub UserForm_Activate()
AccessFormSearch.TextBox1.Value = ""
AccessFormSearch.TextBox1.SetFocus
End Sub
Private Sub UserForm_Initialize()
AccessFormSearch.TextBox1.SetFocus
With AccessFormSearch
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
End With
End Sub