VBA - inconsistent date format

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
I store dates in the format of dd-mm-yyyy. When searching a date, my VBA code always reports date not found. So, I put in two message boxes to check the date format (see the following code). Both boxes report date as, say, 10-7-2011. However, when I open the spreadsheet and select find, the date in the find windows is the format of mm/dd/yyyy, e.g. 7/10/2011. No wonder the date is not found.

What causes the inconsistency and how do I rectify the problem?

=======================================
MsgBox ("date is " & voucherDate)
............
............
Set cell = rng2.Find(voucherDate, lookat:=xlWhole)
If Not r Is Nothing Then
MsgBox (voucherDate & " found")
Else
MsgBox (voucherDate & " not found")
End If
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I think the problem is that you are searching for a Text string that looks like a date but is only Text. You need to search for a Serial Date.

Dates And Times In Excel

If you .Find a Serial date and not a text-date, then the format of the date in the cells shouldn't matter.

Example:
Code:
    [COLOR="Red"]Dim voucherDate As Date[/COLOR], cell As Range

    [COLOR="Red"]voucherDate = DateValue("July 10, 2011")[/COLOR] [COLOR="Green"]' you could use almost any date format to define voucherDate
    'voucherDate = DateValue("7/10/2011")
    'voucherDate = DateValue("10-Jul-11")[/COLOR]

    MsgBox ("date is " & voucherDate)

[COLOR="Green"]    'This will find the cell with the voucher date irregardless of the cell's date format
    ' because voucherDate is a serial date[/COLOR]
    Set cell = rng2.Find(voucherDate, lookat:=xlWhole)
    If Not cell Is Nothing Then
        cell.Select
        MsgBox (voucherDate & " found")
    Else
        MsgBox (voucherDate & " not found")
    End If
 
Upvote 0
Solution

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,923
Latest member
JackiG

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