【问题标题】:Excel cannot find dates (regardless of formatting options)Excel 找不到日期(无论格式选项如何)
【发布时间】:2014-07-16 19:58:24
【问题描述】:

我的老板保留了一个小型 Excel 文件,该文件模仿了我们用来跟踪工作中随机事物的台历,例如小时数和发送报告的数量。它以 7x5 的网格布局(大约 5 个,最后一行只有几个单元格,因为月份通常不能被 7 整除)。

日期和这样的工作方式是第一个单元格 A1 具有该月的第一个日期。随后的每个单元格都是由我的老板手动编码的,以获取它前面的瓦片中日期单元格的值并加 1。但是,这些单元格不是并排的,它们是展开的。我试图将每周的总小时数(和报告)相加,但是当我尝试调用 Cells.Find 方法时,它不会返回任何内容。

我认为可能是因为单元格以mm/dd/yy 的美国方式格式化,所以我尝试搜索05/12/14,但没有成功。所以我试图搜索Date()函数返回的默认日期格式m/d/yyyy,但它仍然没有找到任何东西。我试图设置.Find() 来检查值和公式,但都不起作用。即使我使用ctrl+F 手动找到它们,它也会告诉我工作表中没有这样的值。

我四处搜索,发现没有一个帖子有有效的解决方案 - Cells.Find 方法总是在第一次调用时返回 A1,或者在每次后续调用时返回 Nothing .

我的代码目前很少,但在这里。

代码

 Sub Worksheet()
    Dim r As Range, d As Date, fwkn As Integer, lwkn As Integer, wkn As Integer, col As Collection
    'The purpose of this is to get the week totals using the below functions.
    
    d = DateValue([A1].Value)
    fwkn = WeekNum(d) 'First week number
    d = DateSerial(Year(d), month(d) + 1, 1) - 1 'Gets us the last day of the month
    lwkn = WeekNum(d) 'Last Week Number
    last_of_month = Day(d)
    For i = 1 To last_of_month
    
        d = DateSerial(Year(d), month(d), i)
        Set r = Cells.Find(What:=d, LookIn:=xlFormulas, LookAt:=xlWhole, After:=Range("a1"))
        If Not r Is Nothing Then
            Debug.Print r.Address
        End If
    
    Next
    
End Sub
Function WeekNum(d As Date) As Integer
' You can see examples of this at
' http://www.cpearson.com/excel/weeknum.htm
   WeekNum = CInt(Format(d, "ww", vbMonday))
End Function

有人知道为什么 Excel 找不到日期吗?

【问题讨论】:

  • 您是否查看了单元格中的实际内容以及正在应用的格式规则?
  • 是的,我做到了。除了 A1 单元格之外,所有单元格都是 A1+1E1+1、...、Y1+1A14+1、...,并且都格式化为 mm/dd/yy
  • 如前所述,我在 VBA 窗口和程序本身的常规“查找”窗口中都尝试了 xlValues 和 xlFormulas。
  • 您正在搜索日期 - Excel 将日期内部存储为数字。要获取代表日期的数字(并能够在电子表格中找到它),请使用 CLng
  • @SeanCheshire,试过了,没有找到任何结果(现在它在 A1 中找到了值,但在其他地方没有)

标签: vba date excel


【解决方案1】:

这是一个使用循环查找日期的示例。日期有多种格式,包括文本:

代码:

Option Explicit
Sub FindDatesLooping()
    'Some date in A1
    Dim R As Range, C As Range
    Dim D As Date
    Dim I As Long

Set R = ActiveSheet.UsedRange
D = CDate([A1])

For I = 1 To Day(DateSerial(Year(D), Month(D) + 1, 0))
    D = DateSerial(Year(D), Month(D), I)
        For Each C In R
            If D = C Then
                Debug.Print C.Address, D
            End If
        Next C
Next I
End Sub

在立即窗口中返回:

$C$3          5/3/2014 
$E$5          5/15/2014 
$D$7          5/20/2014 
$A$1          5/28/2014
$G$8          5/31/2014

即时窗口中的日期采用美国格式 m/d/yyyy;但如果我更改我的区域设置,即时窗口日期将以任何格式返回(并且仍然返回所有五个日期)。

【讨论】:

  • 谢谢。输入“15-5-14”会产生文本并且找不到。这是因为我的区域设置吗? (我在怀俄明州)。
  • @dcromley 是的。上面唯一的日期是文本是这样标记的。其他的输入为真实日期并应用了不同的格式。
【解决方案2】:

这似乎有效:

Set r = Cells.Find(what:=DateValue(d), LookIn:=xlFormulas, LookAt:=xlWhole, After:=Range("a1"))

发件人:Chip Pearson http://www.cpearson.com/excel/DateTimeVBA.htm#Finding

编辑:好的,它应该有 LookIn:=xlValues,抱歉

Option Explicit

Sub Main()
  Cells(1, 1) = "5/1/2014"
  Range(Cells(2, 1), Cells(31, 1)).Formula = "=a1+1"
  Dim i&, fwkn&, lwkn&, wkn&, last_of_month&
  Dim r As Range, d As Date
  'The purpose of this is to get the week totals using the below functions.
  d = DateValue([A1].Value)
  fwkn = WeekNum(d) 'First week number
  d = DateSerial(Year(d), Month(d) + 1, 1) - 1 'Gets us the last day of the month
  lwkn = WeekNum(d) 'Last Week Number
  last_of_month = Day(d)
  For i = 1 To last_of_month
    d = DateSerial(Year(d), Month(d), i)
    Set r = Cells.Find(What:=DateValue(d), LookIn:=xlValues, LookAt:=xlWhole, After:=Range("a1"))
    If Not r Is Nothing Then
      Debug.Print r.Address
    End If
    Cells(i, 2) = r.Address
  Next i
End Sub

Function WeekNum(d As Date) As Integer
  ' You can see examples of this at
  ' http://www.cpearson.com/excel/weeknum.htm
  WeekNum = CInt(Format(d, "ww", vbMonday))
End Function

【讨论】:

  • 试过了,还是只输出$A$1;
  • 我在两台不同的电脑上试过了。没有找到任何东西。在 Debug 模式下,它甚至不会到达 Debug.Print 语句。
  • 也许添加第二行。我不知道为什么我不需要那个。 Range(Cells(2, 1), Cells(31, 1)).Formula = "=a1+1"Columns(1).NumberFormat = "m/d/yyyy"
  • 在之前的评论中需要@Jhecht。
  • 为什么要更改 A 列的数字格式?日期并未单独列在该列中。我想我只需要找到一个解决方法,因为即使在程序的查找窗口中,无论我如何搜索,它仍然找不到日期。
【解决方案3】:

范围必须是您要搜索的整个区域,而不仅仅是初始单元格。

set c = Cells.Find(CDate("1/1/2015"), After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

此行应将 c 设置为您要查找的单元格。请注意,没有范围,因为我只是使用 Cells 来引用整个工作表。
您需要保存此地址(如果不是Nothing),因为如果不选中停止,vba 将继续循环。

firstAddress = c.Address 

现在您可以继续查找是否还有更多日期可以找到:

Do 
    'do your stuff here, then look for the next cell
    Set c = .FindNext(c) 
Loop While Not c Is Nothing And c.Address <> firstAddress 

【讨论】:

  • 单元格未设置在范围内,它只是宏所在工作表的单元格属性。无论哪种方式,您的答案都不起作用,它找不到日期“5/2/2014”(这是我更改的代码的唯一内容)
  • 非常感谢,我不确定,但我认为是 LookIn:=xlFormulas 帮助了我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多