Comparing 2 diff Dates in VBA

jfarc

Active Member
Joined
Mar 30, 2007
Messages
316
My brain is fried on this one - I can't get through this 'simple' logic.

I have (2) dates:
aDate
bDate - this will always be equal to or up to 4 or 5 days > (after) aDate

Desired logic:
If the Month of aDate = Month of bDate, then leave both dates as they are.
If the Month of bDate is > the Month of aDate, then keep aDate the same and make bDate = the last day of the month of aDate.

Sample input & results:
Code:
Sample (A) Input
aDate = 09/27/01
bDate = 09/29/01

Sample (A) result
aDate = 09/27/01
bDate = 09/29/01

Code:
Sample (B) Input
aDate = 09/27/01
bDate = 10/02/01

Sample (B) result
aDate = 09/27/01
bDate = 09/30/01

These Dates will not be resident in an Excel worksheet, so I need this code in VBA form. Thank you!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
hm.. let me try my hand at VBA.. anyway, the logic should be that if the month of bDate is greater than the month of aDate, then take the date of bDate, make it the first of the month, then subtract 1 to make it the last day of the previous month.

Public Function ChangeDate

Dim aDate As Date
Dim bDate As Date

If Month(bDate)>Month(aDate) Then
bDate=DateSerial(Year(bDate), Month(bdate), Day(1))-1
End If

End Function
 
Upvote 0
One small change to LxQ's code. If aDate is in December and bDate is in January, then it wouldn't work. Give this a try.

<font face=Courier New><SPAN style="color:#00007F">If</SPAN> Month(bDate) <> Month(aDate) <SPAN style="color:#00007F">Then</SPAN><br>    bDate = DateSerial(Year(bDate), Month(bDate), 0)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN></FONT>
 
Upvote 0
Thanks to both of you!

Yes, I saw the Dec/Jan issue and the ', Day(1)' issue I was having was corrected with the ', 0' replacement.

It's exactly what I was looking for.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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