Finding Dates with different formats

mribadeneira

New Member
Joined
Apr 10, 2013
Messages
7
I am having a problem using Find with VBA to look for dates in a database. The database has the dates in the DD/MM/YYYY format.
The users then select a date they want to find from a dropdown list in a Userform. The dropdown list is made from the database but the format changes to M/D/YYYY. This makes the dates from the userform not match the dates from the database so Find does not work. How can I make VBA shearch for the date regardless of the format?

I have tried playing with it to see if it works but it is still not working. I made the date format in the database match the format in the userform and I made the date selected a String variable. I am using the code:

Code:
Set c = .Find(what:=DateValue(fecha), LookIn:=xlValues)
</pre>
to find it but it only works with some of the dates, most still not working.
It usually works with dates that have two values for day and two values for month (11/10/2012) but not if there is only one value in day or month (5/9/2012).
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi mribadeneira,

Using Range.Find does a Text search which doesn't work well for dates that are not formatted the same.

If the drop down and database are both formatted "DD/MM/YYYY" as it sounds like you tried, then this should work...
Code:
Set c = .Find(what:=fecha, LookIn:=xlValues)

Note that code doesn't use the DateValue function which converts "DD/MM/YYYY" to the default date format on your system.


Another alternative is to use the Application.Match function using the Value2 Property of the Cell that holds your drop down selected date. That will find the first numeric match regardless of the number formatting. The downside of Match is that is not well suited for multiple matches (like Range.FindNext) or searching ranges that are not single column or single row lists.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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