The below code is producing the wrong result. On the work sheet we are using two identical date formats (dd/mm/yyyy) however when the following is run it appears to be trying to interpret the rev_date as American date format, whilst interpreting grid_date as the correct UK format.
We tested this by changing the rev_date on the worksheet to the American format, in which case it produces the correct result.
Any ideas why we would need to change rev_date to an American format, we would prefer to keep it as UK?
Public Function GRIDSALES(rev_date As Date, grid_date As Date) As Double
Dim Order_Type As Range
Dim Final_Price As Range
Dim PaidAlt As Range
Dim Excl_Rev As Range
Dim PAmount1 As Range
Dim PMethod1 As Range
Dim PAmount2 As Range
Dim PayDate2 As Range
Dim PMethod2 As Range
Dim Vstatus As Range
Dim Team As Range
Application.Volatile (True)
Set Order_Type = Sheets("KRONOS").Range("$D:$D")
Set Final_Price = Sheets("KRONOS").Range("$H:$H")
Set PaidAlt = Sheets("KRONOS").Range("$I:$I")
Set Excl_Rev = Sheets("KRONOS").Range("$K:$K")
Set PAmount1 = Sheets("KRONOS").Range("$O:$O")
Set First_PD = Sheets("KRONOS").Range("$Q:$Q")
Set PMethod1 = Sheets("KRONOS").Range("$R:$R")
Set PAmount2 = Sheets("KRONOS").Range("$T:$T")
Set PayDate2 = Sheets("KRONOS").Range("$V:$V")
Set PMethod2 = Sheets("KRONOS").Range("$W:$W")
Set Vstatus = Sheets("KRONOS").Range("$DL:$DL")
Set Team = Sheets("KRONOS").Range("$DO:$DO")
GRIDSALES1 = Application.WorksheetFunction.SumIfs( _
PAmount1 _
, Team, "<>9" _
, Vstatus, "<>rejected", Vstatus, "<>unverified" _
, Excl_Rev, "<>1" _
, PMethod1, "<>Credit" _
, PMethod1, "<>Amendment" _
, PMethod1, "<>Pre-paid" _
, First_PD, ">=" & rev_date _
, First_PD, "<=" & Application.WorksheetFunction.EoMonth(grid_date, 0))
GRIDSALES = GRIDSALES1
End Function
When you do this
and this
you are implicitly coercing two variables of type
Date(rev_dateandgrid_date) into typeString. Implicit coercion is bad practice, and this is a pretty typical example why it’s bad practice. Your dates should be explicitly converted toStringusing an unambiguous format, e.g.In this format, 12 Jan 2011 can’t be confused with 1 Dec 2011.