Hi
I am using this code to delete cells from a spreadsheet if the employee name entered in the InputBox matches a name in column D. Does anyone know how I can amend the code so that if the user enters a misspelled name for example, there will be a message box warning ("Invalid Name Entered!"), which the user can press OK and return to the Input Box and try again.
Sub deleter()
Sheets("Staff").Select
Dim Name
Name = Application.InputBox("Please enter employee name", "Staff Termination")
If Name = "" Then
MsgBox "No name entered"
If Name = False Then
End If
Exit Sub
End If
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long
Dim lastrow As Long
Dim i As Long
lastrow = Sheets("Staff").[D65536].End(xlUp).Row
For i = lastrow To 2 Step -1
If Sheets("Staff").Cells(i, 4) = Name Then
Set SourceRange = Sheets("Staff").Rows(i).EntireRow.Range("A1:R1")
Set DestSheet = Sheets("Termination")
Lr = DestSheet.Cells(Rows.Count, "D").End(xlUp).Row
Set DestRange = DestSheet.Range("A" & Lr + 1)
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Sheets("Staff").Rows(i).EntireRow.Range("A1:R1").Delete shift:=xlUp
End If
Next i
Any help would be appreciated.
Thanks
R
I am using this code to delete cells from a spreadsheet if the employee name entered in the InputBox matches a name in column D. Does anyone know how I can amend the code so that if the user enters a misspelled name for example, there will be a message box warning ("Invalid Name Entered!"), which the user can press OK and return to the Input Box and try again.
Sub deleter()
Sheets("Staff").Select
Dim Name
Name = Application.InputBox("Please enter employee name", "Staff Termination")
If Name = "" Then
MsgBox "No name entered"
If Name = False Then
End If
Exit Sub
End If
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long
Dim lastrow As Long
Dim i As Long
lastrow = Sheets("Staff").[D65536].End(xlUp).Row
For i = lastrow To 2 Step -1
If Sheets("Staff").Cells(i, 4) = Name Then
Set SourceRange = Sheets("Staff").Rows(i).EntireRow.Range("A1:R1")
Set DestSheet = Sheets("Termination")
Lr = DestSheet.Cells(Rows.Count, "D").End(xlUp).Row
Set DestRange = DestSheet.Range("A" & Lr + 1)
SourceRange.Copy
DestRange.PasteSpecial xlPasteValues, , False, False
Application.CutCopyMode = False
Sheets("Staff").Rows(i).EntireRow.Range("A1:R1").Delete shift:=xlUp
End If
Next i
Any help would be appreciated.
Thanks
R