thangvc91
thangvc91

Reputation: 333

VBA compare date from the inputbox with the date in the sheet

I would like to use the date from inputbox , then compare it with the date in the excel sheet , so my code as below :

Sub test()
        Dim date1 As Date
        Date = InputBox("input the date ")
        If date1 = Sheet3.Range("A2").Value Then
            Debug.Print "TRUE"
        Else
                Debug.Print "FALSE"
        End If
        
End Sub

the result it got is always "FALSE" in the immediate box although i have set the date match with the date inputted from inputbox , can you please assist on this case ? Any assist is appreciated.

Upvotes: 1

Views: 187

Answers (2)

Алексей Р
Алексей Р

Reputation: 7627

A short solution, protected against the error of mismatching the type of input data and the data on the sheet (text or error instead of date in a cell)

Debug.Print IIf(InputBox("Input the date: ") = Sheet3.Range("A2").Text, "TRUE", "FALSE")

Upvotes: 0

Gustav
Gustav

Reputation: 55831

The InputBox always returns a string, so convert this:

Sub test()

    Dim Date1 As Date
    Dim Value As String

    Value = InputBox("Input the date")

    If IsDate(Value) Then
        Date1 = DateValue(Value)
        If DateDiff("d", Date1, Sheet3.Range("A2").Value) = 0 Then
            Debug.Print "TRUE"
        Else
            Debug.Print "FALSE"
        End If
    Else
        Debug.Print "N/A"
    End If
        
End Sub

Upvotes: 2

Related Questions