I need to find the first cell which contains a datetime value.
The cell I am interested in displays this
06-Mrz-2015 00:00
It's formula field has this (two spaces after the date part)
06.03.2015 00:00:00
The cell format is this
DD-MMM-YYYY hh:mm
I tried this but it does not find the cell
Public Function FindCurrentCell() As Range
Dim cell As Range
Dim current As String
Sheets("Futures").Select
current = Format(Date, "dd.mm.yyyy") + " 00:00:00"
Set cell = Cells.Find(What:=current, After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
Dim test As String
test = cell.Address
Set FindCurrentCell = cell
End Function
If you search the web you will find many sites that tell you how to find a date. However, they present the answer as a magic spell: “Do this complicated series of steps and it will work.” The trouble is: what do you do if it does not work?
Apparently you need:
.Cells.Find(What:=DateValue(Format(DateWanted, "dd/mm/yyyy")))
where "dd/mm/yyyy" represents Excel's standard date format for your country.
No variation of "dd/mm/yyyy" that I can discover will allow me to successfully find dates formatted as you wish.
The source of the problem is how Excel stores dates. As I type this, it is 9:23 on 7 March 2015. If I type ? CDbl(Date)
? CDbl(Date)
and CDbl(Now())
into the Immediate Window, I get:
? CDbl(Date)
42070
? CDbl(Now())
42070.3911805556
42070 is the number of dates since 1 Jan 1900. .3911 is (Number of seconds since midnight) divided by (Number of seconds in a day).
So if I see “7 March 2015” or “7/3/15” in a cell, I know that, behind the scenes, Excel is holding 42070 with .NumberFormat
telling it that 42070 is a date and how to display that date.
The experts tell you that in DateValue(Format(DateWanted, "dd/mm/yyyy")))
you need to get “dd/mm/yyyy” just the way Excel expects a date to be formatted. The trouble with this statement is that DateValue
will discard all your careful formatting and return a date so what was the point?
All this advice about the format is not only rubbish but, apparently, harmful rubbish. All that matters is that the “What value” is of Date type. For example, both the following are valid:
Cells.Find(What:=Date)
Dim SearchDate As Date: SearchDate = Date
Cells.Find(What:=SearchDate)
I was going to remind you that the Date must be an exact match; for example, a search for “7 March 2015” will not find “7 Match 2015 9:00”. In fact, this is untrue. It appears the oft repeated advice to this effect is only true if you fuss with the formatting.
Sub Test()
Dim RngToday As Range
Set RngToday = FindCurrentCell
If RngToday Is Nothing Then
Debug.Print "Today's date not found"
Else
Debug.Print "Today’s date in " & RngToday.Address
End If
End Sub
Public Function FindCurrentCell() As Range
Dim cell As Range
With Worksheets("Futures")
Set cell = .Cells.Find(What:=Date, After:=.Range("A1"), LookIn:=xlFormulas, _
SearchOrder:=xlByRows, SearchDirection:=xlNext)
If cell Is Nothing Then
' Today's date not found
' Add any appropriate code
Else
' Today's date found
' Add any appropriate code
End If
End With
Set FindCurrentCell = cell
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.