[英]How to find a date time value in an excel sheet
I need to find the first cell which contains a datetime value. 我需要找到包含datetime值的第一个单元格。
The cell I am interested in displays this 我感兴趣的单元格显示此
06-Mrz-2015 00:00
2015年6月6日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. 其中“ dd / mm / yyyy”代表您所在国家/地区的Excel标准日期格式。
No variation of "dd/mm/yyyy" that I can discover will allow me to successfully find dates formatted as you wish. 我发现的“ dd / mm / yyyy”没有任何变化将使我能够成功地找到所需格式的日期。
The source of the problem is how Excel stores dates. 问题的根源是Excel如何存储日期。 As I type this, it is 9:23 on 7 March 2015. If I type
? CDbl(Date)
当我键入此,它是9:23 2015年3月7日如果键入
? CDbl(Date)
? CDbl(Date)
and CDbl(Now())
into the Immediate Window, I get: ? CDbl(Date)
和CDbl(Now())
进入立即窗口,我得到:
? 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). 42070是自1900年1月1日以来的日期数。.3911是(自午夜以来的秒数)除以(一天中的秒数)。
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. 因此,如果在单元格中看到“ 2015年3月7日”或“ 7/3/15”,我知道在幕后,Excel持有42070并带有
.NumberFormat
告诉它42070是日期以及如何显示该日期。
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. 专家告诉您,在
DateValue(Format(DateWanted, "dd/mm/yyyy")))
您需要像Excel希望格式化日期一样获取“ dd / mm / yyyy”。 The trouble with this statement is that DateValue
will discard all your careful formatting and return a date so what was the point? 此语句的麻烦之处在于
DateValue
会舍弃所有仔细的格式设置并返回日期,这又意味着什么呢?
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.
重要的是“ What value”是Date类型。 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”.
例如,搜索“ 2015年3月7日”将不会找到“ 2015年7月7日匹配”。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.