PDA

View Full Version : Solved: Need to calculate dates



clhare
03-26-2009, 06:55 AM
I have a macro that will get 2 dates from the user. With the first date, I need the macro to assign the fifth of the previous month to a new variable, and with the second date, I need the macro to assign the fifth of the next month to a new variable.

So...

If Date1A = 3/1/2009, I need the macro to assign "2/5/2009" to Date1B.
If Date2A = 4/16/2009, I need the macro to assign "5/5/2009" to Date2B.Is this possible? I don't have any idea how to do this!

fumei
03-26-2009, 01:20 PM
Is this possible?

Yes.

Start by looking up how dates are handled by VBA. It is in Help.

macropod
03-26-2009, 01:29 PM
Hi Cheryl,

If you're soliciting the first date via a text formfield, you don't even need vba for this - a simple field calculation will do.

clhare
04-02-2009, 04:59 AM
I'm getting the dates from a userform, not a formfield. There are actually 2 different calculations I'm trying to figure out. I'm still trying to figure out how to do the one mentioned above.

I thought I figured out the second calculation (below), but it's not working as expected. Can anyone tell me how to correct this one?

For calculation #2, I want "strDate3 = Max of (strDate2 and (strDate1 + 2 months))".


strDate1 = "03/26/2009"
strDate2 = "11/01/2008"

' Add 2 months to strDate1
strDate1 = DateAdd("m", 2, strDate1)

' Assign maximum date to strDate3
If strDate2 >= strDate1 Then
strDate3 = strDate2
ElseIf strDate1 > strDate2 Then
strDate3 = strDate1
End If


When I run this code, the minimum of the two dates ends up in strDate3 instead of the maximum. I have no idea what I am doing wrong.

clhare
04-02-2009, 06:06 AM
I think I figured out Calculation #2. I had declared the date variables as strings instead of dates. When I changed them to dates variables, the calculation works correctly. Does that sound right?

fumei
04-02-2009, 10:00 AM
Yup.

This is a common misunderstanding. Dates are dates, and strings are strings. Just because it looks like a date does not make something a Date.