date not found in range

Jupiler

New Member
Joined
Jun 10, 2003
Messages
49
Hello, excellians. In range A1 to A45 I've got dates starting from 22-1-2009 til 25-2-2009.
When I try this procedure and I enter a value like 22-2-2009, this date is never found.
Can anybody show me the error in next procedure.

Sub tryout()
Dim myrange As Range
Dim mycell As Range
Dim dSearch As Date


dSearch = Now()
dSearch = InputBox("enter date")
Set myrange = Range("a1:a45")

On Error Resume Next
Set mycell = myrange.Find(what:=dSearch, searchformat:="ddmmyyyy", lookat:=xlWhole)
On Error GoTo 0
If Not mycell Is Nothing Then
MsgBox "ok"
Else
MsgBox "nothing found"
End If
End Sub


Thanks in advance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try without the searchformat bit:

Code:
Sub tryout()
Dim myrange As Range
Dim mycell As Range
Dim dSearch As Date

dSearch = Now()
dSearch = InputBox("enter date")
Set myrange = Range("a1:a45")
On Error Resume Next
Set mycell = myrange.Find(what:=dSearch, lookat:=xlWhole)
On Error GoTo 0
If Not mycell Is Nothing Then
MsgBox "ok"
Else
MsgBox "nothing found"
End If
End Sub

Dom
 
Upvote 0
This worked for me:

Code:
Sub tryout()
    Dim myrange As Range
    Dim mycell As Range
    Dim dSearch As Date
    dSearch = Now()
    dSearch = InputBox("enter date")
    Set myrange = Range("a1:a45")
    On Error Resume Next
    Set mycell = myrange.Find(what:=DateValue(dSearch), LookIn:=xlFormulas, lookat:=xlWhole)
    On Error GoTo 0
    If Not mycell Is Nothing Then
        MsgBox "Found in " & mycell.Address
    Else
        MsgBox "nothing found"
    End If
End Sub
 
Upvote 0
Sorry, doesn't work. Maybe it's something with the format. Maybe I should try first with ordinary strings instead of date.
Thanks anyway.
 
Upvote 0
Find can be tricky with dates, particularly if you are in the UK like me. However, to test my code I entered the 22 Jan 2009 to 25 Feb 2009 in A1:A35 and formatted the cells as dd-mm-yyyy. Whe I ran my code and entered 22-2-2009, the message box said "Found in $A$32".

Are you sure your dates are serial dates not text? Other than that possibility, I don't know why it's not working for you, sorry.
 
Upvote 0
I started all over again and ... now it works. Thank you Mr. Poulsom. Seems that even
in Belgium find with dates can be tricky.
Greetings!
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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