So I have a short VBA script that runs through all of my data in one sheet and determines if the first column (last name) matches a user specified (via form text box) last name, then the second column (first name) matches a user specified first name, and takes all rows which match and pastes them into a second sheet. This works great. I also have a form text box that allows the user to specify how far back they want data in years. The 5th column (E) has dates in it. I have tried several things and they always throw errors at me. Most commonly I get a mismatch error when trying to initialize a date from the cell as a date, which seems like it should just already be.
Here is all of the code
Private Sub cbGetResults_Click()
Dim LastName As String
Dim FirstName As String
Dim CellLast As String
Dim CellFirst As String
Dim LastRow As Long
Dim YearsBack As Long
Dim CellDate As Date
Dim FindDate As Date
With ActiveSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
LastName = tbLastName.Text
FirstName = tbFirstName.Text
YearsBack = tbYearsBack.Value
Dim i As Long, j As Long
j = 1
For i = 4 To LastRow
With ActiveSheet
CellLast = .Range("A" & i).Text
CellFirst = .Range("B" & i).Text
'CellDate = .Range("E" & i).Value
End With
FindDate = DateAdd("yyyy", -YearsBack, dtCurrent)
If UCase(CellLast) = UCase(LastName) Then
If UCase(CellFirst) = UCase(FirstName) Then
'If CellDate >= FindDate Then
Worksheets("Page1").Rows(i).Copy Destination:=Worksheets("Recent").Range("A" & j)
j = j + 1
'End If
End If
End If
Next
GetRecentInfrac.Hide
End Sub
The parts I have commented out are the parts that deal with the dates. I get errors when they aren't commented. As it is now, the line
'CellDate = .Range("E" & i).Value
gives me a mismatch error. Currently the entire column E is stored as a date. I even went through each row and checked for poor data entry. I found some blank cells and thought that might be it but no luck still. I have also tried evaluating the dates as integers to see if that would work but I got some weird errors that highly discouraged that path.
Any suggests on how best to evaluate if the cells date is older than the specified years back.
For clarification, if the user inputs 3 into the YearsBack textbox, then I want to see if a cell in the sheet is greater than today's date - 3 years.
Thanks,
Troll
Bookmarks