I have the code below and if the user adds a date it works fine but if he adds a number, e.g. 1 or 66, instead of the date the code still runs and deletes all the columns. I can't seem to fix this.
Sub deleteCols()
Dim early As Date
Dim late As Date
Dim lc As Long
Dim i As Long
Application.ScreenUpdating = False
On Error GoTo Errorhandler
early = CDate(Application.InputBox(Prompt:="Please enter start date:", Type:=2))
late = CDate(Application.InputBox(Prompt:="Please enter end date:", Type:=2))
If early = 0 Then Exit Sub
If late = 0 Then Exit Sub
lc = Cells(1, Columns.Count).End(xlToLeft).Column
For i = lc To 8 Step -1
If Cells(1, i) < early Then
Cells(1, i).EntireColumn.delete
ElseIf Cells(1, i) > late Then
Cells(1, i).EntireColumn.delete
Else
End If
Next i
Application.ScreenUpdating = True
Exit Sub
Errorhandler:
MsgBox "You need to insert a date dd/mm/yyyy"
Resume
End Sub