Comparing the value of two cells with VBA: not working

Kentetsu

Well-known Member
Joined
Jan 22, 2004
Messages
520
Attempting to compare two cells to see if they match. One has a date entered by the user, the other has a date provided by a Vlookup. I can't seem to get it to recognize an actual match no matter what I try.

Code:
If .Range("B11").Value <> .Range("C11").Value Then
        MsgBox "Code Date Info does not Match!", vbOKOnly
        Exit Sub
End If

I've played around with the formatting of both cells to no avail. Thanks for your thoughts on this...
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
It's possible that one has time appended to it, thus not allowing them to be matched. It also might be that the user is inputting it in such a way that it's not a date format anymore. To get around these potential errors:

Code:
        If Round(CLng(.Range("B11").Value), 0) <> Round(CLng(.Range("C11").Value), 0) Then            MsgBox "Don't Match", vbOKOnly
        End If
This should convert them to the same format and round off any potential time additions. Other than that, I don't know.
 
Upvote 0
Attempting to compare two cells to see if they match. One has a date entered by the user, the other has a date provided by a Vlookup. I can't seem to get it to recognize an actual match no matter what I try.

Code:
If .Range("B11").Value <> .Range("C11").Value Then
        MsgBox "Code Date Info does not Match!", vbOKOnly
        Exit Sub
End If

I've played around with the formatting of both cells to no avail. Thanks for your thoughts on this...
Is what you posted within a With-End With construct that references the sheet those ranges are on? If yes, try this test:
In the Immediate Window type:
?IsDate(Activesheet.Range("B11")) and press enter - what do you get?
Repeat with C11 substituted for B11 - what do you get?
 
Upvote 0
Hi Joe! :)

You are correct in your assumption that this is contained within a With-End With construct.

I tried your test, and it came up True.
I tried the same test on C11 and it also came up True.

Thanks for your help. Here's the entire construction so far:

Code:
Option Explicit
Public Sub VerifyEntry()

With Sheets("Sheet1")
    If .Range("B8").Value <> .Range("C8").Value Then
        MsgBox "Piece Printed Package Info does not Match!", vbOKOnly
        Exit Sub
    End If
        
    If .Range("B9").Value <> .Range("C9").Value Then
        MsgBox "Case Printed Package Info does not Match!", vbOKOnly
        Exit Sub
    End If
        
    If .Range("B10").Value <> .Range("C10").Value Then
        MsgBox "Case Number Info does not Match!", vbOKOnly
        Exit Sub
    End If
        
    If .Range("B11").Value <> .Range("C11").Value Then
        MsgBox "Code Date Info does not Match!", vbOKOnly
        Exit Sub
    End If

    If .Range("B13").Value <> .Range("C13").Value Then
        MsgBox "Establishment Number Info does not Match!", vbOKOnly
        Exit Sub
    End If
        
End With


End Sub

All other checks perform as expected, only the Code Date check is throwing up unexpected results...
 
Upvote 0
Then as suggested in post #2, if you only want to compare the calendar dates (ignoring the exact time of day for those dates) try changing the offending lines to this:
Code:
If Int(.Range("B11").Value) <> Int(.Range("C11").Value) Then
        MsgBox "Code Date Info does not Match!", vbOKOnly
        Exit Sub
End If
 
Upvote 0
My apologies NeonRedSharpie, I totally missed your response. That did the trick to get it working.

Thank you for your assistance, both of you.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

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