IntraCell Date Comparison

poorwallace

Active Member
Joined
Mar 8, 2005
Messages
365
Hello all....I've got a question about comparing 2 dates. The problem is that the date is contained within a cell in this format:

NNGGQ 12/21/2005

I'm trying to compare the date in the right of the cell to the date in another cell using this:

Code:
If Right(Cells(4, 4), 8) = Range("G1") Then
    MsgBox "It's today!"
ElseIf Right(Cells(4, 4), 8) <> Range("G1") Then
    MsgBox "It's not today!"
End If


From the immediate window, I'm getting this:

Code:
?Right(Cells(4, 4), 8)
12/21/05
?Range("G1")
12/21/2005

I suspect the problem is that Range G1 is formatted as a date, while the Right(Cells...) bit is returning text. I could create another cell to output the results of the Right(Cells), but I'd like to see if anyone has an easier solution.

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Code:
If CDate(Right(Cells(4, 4), 8)) = Range("G1") Then
    MsgBox "It's today!"
Else
    MsgBox "It's not today!"
End If

Regards,
 
Upvote 0
Why are you pulling 8 characters when your date is in a 10 character format?

Code:
Sub test()
If DateValue(Right(Range("D4"), 10)) = Range("G1") Then
    MsgBox "It's today!"
Else
    MsgBox "It's not today!"
End If
End Sub
 
Upvote 0
Try using DateValue to convert to a 'real' date.
 
Upvote 0
poorwallace said:
Barrie,

You've helped me again! What just happened? I'm not familiar with CDate...

Thanks!

Cdate will attempt to convert a string variable to a date variable.

Regards,
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,836
Members
449,096
Latest member
Erald

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