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 = Shee开发者_如何学Cts("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
First_PD, ">=" & rev_date
and this
First_PD, "<=" & Application.WorksheetFunction.EoMonth(grid_date, 0))
you are implicitly coercing two variables of type Date
(rev_date
and grid_date
) into type String
. Implicit coercion is bad practice, and this is a pretty typical example why it's bad practice. Your dates should be explicitly converted to String
using an unambiguous format, e.g.
First_PD, ">=" & Format$(rev_date,"dd mmm yyyy")
In this format, 12 Jan 2011 can't be confused with 1 Dec 2011.
I am not sure of exactly why the date format changes but from googling in the past I am fairly certain that VBA will interpret a date as American which may be the issue.
In your function, could you take each date and try and format to UK date format? Example:
Public Function GRIDSALES(rev_date As Date, grid_date As Date) As Double
Dim d1 as Date, d2 as Date
d1 = CDate(Format(rev_date, "dd-mm-yyyy"))
d2 = CDate(Format(grid_date, "dd-mm-yyyy"))
///rest of your code here...
End Function
Thanks, my error was just that.
But i solved it like this
fil1 = "<=" & Month(rev_date) & "/" & Day(rev_date) & "/" & Year(rev_date)
精彩评论