如何在Excel工作表中finddate时间值

我需要find包含date时间值的第一个单元格。

我感兴趣的单元显示这个

06-Mrz-2015 00:00

公式字段有这个(date部分之后的两个空格)

06.03.2015 00:00:00 

单元格格式是这样的

 DD-MMM-YYYY hh:mm 

我试过这个,但是没有find这个单元格

 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 

如果你在网上search,你会发现许多网站,告诉你如何find一个date。 然而,他们把答案当成魔法:“做这个复杂的一系列步骤,它会起作用的。”问题是:如果不起作用,你会怎么做?

显然你需要:

 .Cells.Find(What:=DateValue(Format(DateWanted, "dd/mm/yyyy"))) 

其中“dd / mm / yyyy”代表您所在国家/地区的Excel的标准date格式。

没有变化的“dd / mm / yyyy”,我可以发现将允许我成功find您想要的格式。

问题的根源在于Excel如何存储date。 当我input这个时间,在2015年3月7日9:23。如果我input? CDbl(Date) ? CDbl(Date)CDbl(Now())到立即窗口,我得到:

 ? CDbl(Date) 42070 ? CDbl(Now()) 42070.3911805556 

42070是自1900年1月1日以来的date数.3911是(自午夜以来的秒数)除以(一天中的秒数)。

所以,如果我在一个单元格中看到“2015年3月7日”或“7/3/15”,我知道在幕后,Excel使用.NumberFormat持有42070,告诉它42070是date以及如何显示该date。

专家告诉你,在DateValue(Format(DateWanted, "dd/mm/yyyy")))你需要得到“dd / mm / yyyy”,就像Excel预期date的格式一样。 这个陈述的问题在于DateValue会放弃所有你认真的格式并返回一个date,所以重要的是什么?

所有关于这种格式的build议不仅是垃圾,而且显然是有害的垃圾。 重要的是datetypes的“什么值”。 例如,以下两个都是有效的:

 Cells.Find(What:=Date) Dim SearchDate As Date: SearchDate = Date Cells.Find(What:=SearchDate) 

我要提醒你date必须完全匹配; 例如,search“2015年3月7日”将不会find“7匹配2015年9:00”。 其实这是不真实的。 这似乎是经常反复的build议,这种效果只有如果你大惊小怪的格式。

 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