Ask Experts Questions for FREE Help !
Ask
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #1

    Jan 11, 2011, 01:26 PM
    Comparing Date Values in VBA
    Jerry, you'll probably be the one to respond so I'll just post this for you.

    I'm trying to compare some date values using VBA and the issue I'm having is that my code is treating date values that are older than more recent values as greater. So, if you were to run the code below you'd see that it's returning EOPM instead of BOCM. Well, that can't be right because Dec 31, 2010 is not a greater date value than 1/1/11.

    I won't get into what I'm using this for or any of that because it would take me awhile to explain that and I don't have that kind of time. If you have any suggestions, I'd greatly appreciate it.

    BOPM = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mm.dd.yy") ' Begin of Prev Month
    EOPM = Format(DateSerial(Year(Date), Month(Date), 0), "mm.dd.yy") 'End of Prev Month
    BOCM = Format(DateSerial(Year(Date), Month(Date), 1), "mm.dd.yy") 'Begin of Curr Month
    EOCM = Format(DateSerial(Year(Date), Month(Date) + 1, 0), "mm.dd.yy") 'End of Curr Month
    MRD = Format(Date - 1, "mm.dd.yy")

    date1 = Format(Date - 1, "mm.dd.yy")
    date2 = Format(Date - 2, "mm.dd.yy")
    date3 = Format(Date - 3, "mm.dd.yy")

    If BOCM > EOPM Then

    MsgBox ("BOCM")

    Else

    MsgBox ("EOPM")

    End If


    End Sub
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #2

    Jan 11, 2011, 01:43 PM

    The problem is that you aren't comparing date values. By using Format, you are converting the date values to text strings. You should use Format only when you are ready to display the value.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #3

    Jan 11, 2011, 02:45 PM

    Actually, Excel is pretty good, sometimes, at doing <> comparisons on text strings, too. I'd suggest that your non-standard format of "mm.dd.yy" is just too far to go. You might find that Excel gets it right if you use a standard date format like "mm/dd/yy", and almost 100% sure if you use the data format that matches your system's default date format.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #4

    Jan 11, 2011, 02:53 PM

    Note, you can eliminate the whole issue by using Excel's VarType of Date. Declaring and using variables as dates removes any need to fiddle with formats of any kind, as Scott has already pointed out.

    Using your example:
    Code:
    Dim BOPM As Date, BOCM As Date
    Dim EOPM As Date, EOCM As Date, MRD As Date
    Dim Date1 As Date, Date2 As Date, Date3 As Date
    
    BOPM = DateSerial(Year(Date), Month(Date) - 1, 1)   'Begin of Prev Month
    EOPM = DateSerial(Year(Date), Month(Date), 0)       'End of Prev Month
    BOCM = DateSerial(Year(Date), Month(Date), 1)       'Begin of Curr Month
    EOCM = DateSerial(Year(Date), Month(Date) + 1, 0)   'End of Curr Month
    MRD = Date - 1
    
    Date1 = MRD
    Date2 = Date - 2
    Date3 = Date - 3
    
    If BOCM > EOPM Then MsgBox "BOCM" Else MsgBox "EOPM"

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

Comparing Percentages with certain values [ 2 Answers ]

Say that I have two percentages each with its own value, we will set the value in money 94% of $8 and 63% of $17 How do I compare which one is going to end up producing the most money

VBA function for Date [ 1 Answers ]

Ok, here's my problem. I am trying to concatenate a Chart Title with a date in the MMM-YY format. I have to create a Chart in excel each month. If I create the chart in December, it would be for November's data. I can't figure out how to concatenate "Clinical Utilization: " &...

Comparing to the ex [ 8 Answers ]

I am having a really hard time dating... can anyone help me out. I feel like I fall for every girl I date. I've been single for about 6 months after a 3.5 year relationship that included: living together, cooking, romance, GREAT sex life, talking daily, sharing deepest secrets, cuddling, etc... ...

Excel vba returning values from a sub [ 2 Answers ]

How do I return values from a subroutine in Excel VBA? It appears that I have to "temporarily" store a value elsewhere on the spreadsheet and recover when I return to the main part of the program. Simply the program runs and asks the user to enter data on several forms, but this data has to be...

Comparing to Ex! [ 3 Answers ]

So, I have been broken up with my ex for a while now, but I am having trouble moving onto other people. I have met, and gone on dates with a few women since the break up. I always find myself comparing it to my ex. I was very much in love with her, and she is the one that dumped me. All of the...


View more questions Search