user3434646
user3434646

Reputation: 13

How to convert a custom date to date for a camparison?

I need to convert a custom date data from Spreadsheet 1 to make a comparison with a date data from Spreadsheet 2. The custom date data has this format: 21/02/2014 08:09:21 a.m., and will be stored in variable Date1. And the date data is: 21/02/2014, and stored in Date2. I need to compare only the dates (without the time parameter) with the instruction "if", e.g.

If Date1=Date2 Then

   'Instruction

End If

I have most of the code, so I basically just need the way to convert the custom date data to date data, store in the variables and compare them.

Upvotes: 1

Views: 48

Answers (1)

Automate This
Automate This

Reputation: 31364

VBA solution:

enter image description here

Sub timeCompare()
    date1 = Application.WorksheetFunction.Text([A1], "d/m/yyyy")
    date2 = Application.WorksheetFunction.Text([A2], "d/m/yyyy")

    If date1 = date2 Then
        MsgBox ("match")
    Else
        MsgBox ("no match")
    End If
End Sub

OR

Sub timeCompare()
    date1 = Int([A1])
    date2 = Int([A2])

    If date1 = date2 Then
        MsgBox ("match")
    Else
        MsgBox ("no match")
    End If
End Sub

Results:

enter image description here


Formula solution:

=IF(TEXT(A1,"d/m/yyyy")=TEXT(A2,"d/m/yyyy"),"Match","No Match")

Or

=IF(INT(A1)=INT(A2),"Match","No Match")

Upvotes: 1

Related Questions