Hi I have a data set where i need to filter on 2 criteria and then perform a simple calculation for the remaining records to show the numberof days between 2 dates. I had previously been looping through cells with an if statement to do this but it seems to slow down the code so I'd like to try a different way.
The first part of my code works fine as it's using the same dates all the way down the column:
However, I'm struglling with the 2nd part where I need tofind the number of days between todays date and the cell to the left of the cell my calculation is in. I have the following code but it's giving an "applpication defined or object defined" error. The line .FormulaR1C1 = "= today()- RC[,-1]" seems to be wrong but I'm not sure of the correct way to write it.
Also, is this way of writing the code more efficient than looping? or is there a better way?
thanks
The first part of my code works fine as it's using the same dates all the way down the column:
Code:
ColRef = WS.UsedRange.Find(What:="Certification - Status", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
ColRef2 = WS.UsedRange.Find(What:="Person - Start Date", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
ColRef3 = WS.UsedRange.Find(What:="Days Overdue", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
dDate = DateSerial(2012, 5, 21)
lDate = dDate
With WS.UsedRange
.AutoFilter field:=ColRef, Criteria1:="=Not Yet Complete"
.AutoFilter field:=ColRef2, Criteria1:="<" & lDate
End With
RwLast = WS.Cells(Rows.Count, 1).End(xlUp).Row
WS.Cells(2, ColRef3).Resize(RwLast - 1).Value = DateDiff("d", lDate, Date)
WS.ShowAllData
However, I'm struglling with the 2nd part where I need tofind the number of days between todays date and the cell to the left of the cell my calculation is in. I have the following code but it's giving an "applpication defined or object defined" error. The line .FormulaR1C1 = "= today()- RC[,-1]" seems to be wrong but I'm not sure of the correct way to write it.
Code:
With WS.UsedRange
.AutoFilter field:=ColRef, Criteria1:="=Not Yet Complete"
.AutoFilter field:=ColRef2, Criteria1:=">=" & lDate
End With
RwLast = WS.Cells(Rows.Count, 1).End(xlUp).Row
With WS.Cells(2, ColRef3).Resize(RwLast - 1)
.FormulaR1C1 = "= today()- RC[,-1]"
.FillDown
.Value = .Value
End With
Also, is this way of writing the code more efficient than looping? or is there a better way?
thanks