Excel VBA - corresponding day in previous month

alisonthom
alisonthom used Ask the Experts™
on
Hi
I would appreciate help with trying to return a TRUE/FALSE as to whether there a corresponding day in the previous month.  

For example, if the current day is 25th Feb 2019, return TRUE since there is a corresponding day (25th Jan 2019) in the previous month.  But if the current day is 31st Dec 2018 return FALSE since there is no corresponding day in the previous month.

Thanks in advance
Alison
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Roy CoxGroup Finance Manager

Commented:
Why VBA?

Author

Commented:
It is part of other VBA I am writing
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
You may try this user defined function to test if the same day exists in the previous month.

Function IsDateAvailableInPreviousMonth(dt As Date) As Boolean
    Dim ldt As Date
    On Error Resume Next
    ldt = DateValue(Year(dt) & "-" & Month(dt) - 1 & "-" & Day(dt))
    If Err = 0 Then IsDateAvailableInPreviousMonth = True
    On Error GoTo 0
End Function

Open in new window

Author

Commented:
Hi Subodh,  that is exactly what I was looking for.  Many thanks!
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome Alisonthom! Glad it worked as desired.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial