【问题标题】:EXCEL VBA - Find dates does not work for months with 2 digitsEXCEL VBA - 查找日期不适用于 2 位数月份
【发布时间】:2014-02-11 08:38:06
【问题描述】:

我正在尝试查找某个范围内的日期。这适用于 2 位数的月份(1 月至 9 月),但不适用于其余月份。我花了一些时间来隔离问题,所以我建立了一个测试例程。

这是数据的范围

12.01.2013  
12.02.2013
12.03.2013
12.04.2013
12.05.2013
12.06.2013
12.07.2013
12.08.2013
12.09.2013
12.10.2013
12.11.2013
12.12.2013

这是代码:

With wsData.Range("A1:A12")
For i= 1 To 12

    Set rReturn = .Find(What:=CDate("12." & i & ".2013"), LookIn:=xlValues, LookAt:=xlWhole)

Next i
End With

对于前 9 个循环,我得到了 rReturn 的相应单元格。但是对于最后 3 个循环(10 到 12),返回值是“nothing”。

【问题讨论】:

  • 看看this 我也遇到过类似的情况,使用 Find 功能查找日期时性能非常差,也许它也可以帮助您。
  • cDate 使用您的系统设置的日期,也许您需要更改日期和月份的顺序,将月份放在首位

标签: date excel find vba


【解决方案1】:

问题不在于日期。它们是字符串。此外,您还需要格式化 i,以便您可以构建准确的搜索字符串。

试试这个

Set rreturn = .Find(What:="12." & Format(i, "00") & ".2013", _
                    LookIn:=xlValues, LookAt:=xlWhole)

久经考验

Sub Sample()
    Dim wsData As Worksheet
    Dim SearchString As String
    Dim i As Long

    Set wsData = ThisWorkbook.Sheets("Sheet1")

    With wsData.Range("A1:A12")
        For i = 1 To 12
            SearchString = "12." & Format(i, "00") & ".2013"
            Set rreturn = .Find(What:=SearchString, LookIn:=xlValues, LookAt:=xlWhole)

            If Not rreturn Is Nothing Then _
            Debug.Print SearchString & " found in " & rreturn.Address
        Next i
    End With
End Sub

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多