Hi From Ukraine @Malagon.
Try your code with slight modification:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
Dim iLast As Long
Dim Response As VbMsgBoxResult
Dim employeeEmpID As String, firstName As String, lastName As String
With Target
If .Count > 1 Or .Column <> 1 Or .Row = 1 Then Exit Sub
Application.EnableEvents = False
If .Value = "" Then
.EntireRow.Delete shift:=xlUp
GoTo ExitSub:
End If
Set C = Sheets("Employee").Columns("a").Find(what:=Target, LookAt:=xlWhole)
If C Is Nothing Then
Response = MsgBox("EmpID: " & .Value & " NOT found! " & vbNewLine & "Do you really want to add a new EmpID to the database?", vbQuestion + vbYesNo, "Attention!")
If Response = vbYes Then
' Employee EmpID not found, add a new employee
employeeEmpID = .Value
firstName = InputBox("Enter employee name: ")
lastName = InputBox("Enter the employee's last name: ")
With Sheets("Employee")
iLast = .Columns("A").Find(what:="*", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Row + 1
.Cells(iLast, 1).Value = employeeEmpID
.Cells(iLast, 2).Value = firstName
.Cells(iLast, 3).Value = lastName
End With
' After adding a new employee, fill in the data on the DataEntry sheet
.Offset(, 1).Resize(, 5) = Array(firstName, lastName, Int(Now), Int(Now), Application.UserName)
.Offset(, 3).Resize(, 2).NumberFormat = "mm/dd/yyyy"
.Offset.Resize(, 7).EntireColumn.AutoFit
Else
.Value = ""
GoTo ExitSub:
End If
Else
.Offset(, 1).Resize(, 5) = Array(C(, 2), C(, 3), Int(Now), Int(Now), Application.UserName)
.Offset(, 3).Resize(, 2).NumberFormat = "mm/dd/yyyy"
.Offset.Resize(, 7).EntireColumn.AutoFit
End If
End With
ExitSub:
Application.EnableEvents = True
End Sub
Peace and Health! Good luck.
Bookmarks