Monday, August 10, 2020

VBA Excel How to check the date is same after reversal of day and month

Function isBeforeAfterDateSame(strDateBefore As String) As Boolean
    Dim x As String
    Dim y As String
    Dim z As String
    Dim strInDate As String
    Dim strDateAfter As String
    Dim flag As Boolean
    Dim result As Date
    
    strInDate = strDateBefore
    
    If Len(strInDate) >= 8 And Len(strInDate) <= 10 Then
        '' date format is with /  digit separator
        If VBA.InStr(strInDate, "/") > 0 Then
            If VBA.IsDate(strInDate) Then
            
                x = VBA.Split(strInDate, "/")(0)
                y = VBA.Split(strInDate, "/")(1)
                z = VBA.Split(strInDate, "/")(2)
                
                If Len(x) = 1 Then x = "0" & x
                If Len(y) = 1 Then y = "0" & y
                
                strInDate = x & "/" & y & "/" & z

                result = DateValue(strInDate)
                strOutDate = CStr(result)
            
                x = VBA.Split(strOutDate, "/")(0)
                y = VBA.Split(strOutDate, "/")(1)
                z = VBA.Split(strOutDate, "/")(2)
                
                If Len(x) = 1 Then x = "0" & x
                If Len(y) = 1 Then y = "0" & y
                
                strOutDate = x & "/" & y & "/" & z

                If strOutDate = strInDate Then
                    flag = True
                End If
            End If
        End If
    End If
    
    isBeforeAfterDateSame = flag
    
End Function

No comments:

Post a Comment

Hot Topics