why is this causing a problem?

codesmith

Active Member
Joined
Apr 23, 2008
Messages
257
Code:
Sub testDateCompare()
    Dim nmiDate As String
    nmiDate = CDate("23/4/08")
    Dim origDate As String
    origDate = CDate("23/04/2008 15:05")
    origDate = Left(origDate, InStr(origDate, " "))
    
    Dim minDate As String
    minDate = WorksheetFunction.Min(origDate, nmiDate)
    MsgBox minDate
End Sub


Giving '400' error!?

thanks in advance.
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Jindon, the observation about the time component is astute.

result = Int(Min(a,b)) is more efficiant than result = Min(Int(a),Int(B))


earlier when I converted date to a long format, i was getting a floating point number ... this is why i was lead to believe i need to remove the time component.


and by the looks of it, with time 15.05 .. it was rounding up to next day ... this is why i was getting 21/04/08 as a result!!!

see below:

Code:
Sub testDateCompare()
    Dim nmiDate As Date, origDate As Date, minDate As Date
    nmiDate = CDate("23/4/08")
    origDate = CDate("20/04/2008 15:05")
    minDate = CDate(WorksheetFunction.Min(CLng(origDate), CLng(nmiDate)))
   
    MsgBox Format(CLng(nmiDate), "dd/mm/yyyy") 'result: 23/04/08
    MsgBox Format(CLng(origDate), "dd/mm/yyyy") 'result: 21/04/08
    
    origDate = CDate("20/04/2008 11:05") '##changed
    MsgBox Format(CLng(nmiDate), "dd/mm/yyyy") 'result: 23/04/08
    MsgBox Format(CLng(origDate), "dd/mm/yyyy") 'result: 20/04/08
    
End Sub
 
Last edited:
Upvote 0
Jindon, the observation about the time component is astute.

result = Int(Min(a,b)) is more efficiant than result = Min(Int(a),Int(B))
Read the op's code again.
He wants to compare the dates without time part. (doesn't mean much to me anyway though)
 
Upvote 0
Then try
Rich (BB code):
minDate = CDate(WorksheetFunction.Min(Fix(origDate), Fix(nmiDate)))
 
Upvote 0
as you can see:

Code:
Sub testDateCompare()
    Dim nmiDate As Date, origDate As Date, minDate As Date
    
    nmiDate = CDate("20/4/08")
    origDate = CDate("20/04/2008 15:05")
        
    MsgBox CLng(nmiDate) '39558
    MsgBox CLng(origDate) '39559
    
    
    
    origDate = Left(origDate, InStr(origDate, " "))
    MsgBox CLng(nmiDate) '39558
    MsgBox CLng(origDate) '39558
    
End Sub
 
Upvote 0
hey man, why does it not mean much, obviously i am missing some crucial bit of information here :)

Because you are comparing DATE, which is Double type of data (Numeric).
What's the matter about returning "21/04/2008", when minimun of "21/04/2008" and "21/04/2008 07:20" ?
 
Upvote 0
Because you are comparing DATE, which is Double type of data (Numeric).
What's the matter about returning "21/04/2008", when minimun of "21/04/2008" and "21/04/2008 07:20" ?


there is a reason, if var holding 21/04/2008 07:20 is returned as a greater date than var holding just 21/4/08 ... then it tells me something which i dont want it to.

(it's because of the logic that surrounds this simple routine!!)
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top