Hey there,
I have created a macro to delete any row which has a date older than the date specified by the user. However for some odd reason, when the macro finds a row which has an older date, it doesnt select that date and ends up selecting a completely different row and deletes that.
My macro code is shown below and I have attached my spreadsheet. If anyone has a clue as to why its selecting the wrong row then that would be great as I am quite honestly baffled!
Sub Remove_Old_Data_New()
Dim dateDim As Date, strDate As String, foundRange As Range, rangeDel As Range, i As Long, lastRow As Long
Dim confirmDelete As String
Dim convertedDate As Long
Dim convertedFoundDate As Long
dateDim = Application.InputBox(Prompt:="Please enter the date you wish to filter from: ", _
Title:="Date Filter", Default:=Format(Date, "DD/MM/YYYY"), Type:=1)
If dateDim Then
strDate = Format(dateDim, "DD/MM/YYYY")
Else
MsgBox "Action Cancelled"
End If
MsgBox "The date entered is: " & dateDim, vbInformation, "Remove Old Data"
convertedDate = CDate(strDate)
MsgBox convertedDate
ActiveSheet.Select
convertedFoundDate = CDate(Cells(5342, 14).Value)
With ActiveSheet
For i = 2 To .UsedRange.Rows.Count
convertedFoundDate = CDate(Cells(i, 14).Value)
If convertedFoundDate <= convertedDate Then
.Cells(i).EntireRow.Select
MsgBox convertedFoundDate
MsgBox i
.Cells(i).EntireRow.Delete
End If
Next i
End With
End Sub
For some odd reason I keep getting a database error when I try to upload my spreadsheet but I'll try again once I post this thread.
Thanks,
Jag
Bookmarks