【问题标题】:How to use .Find in VBA to find a specific data type?如何在 VBA 中使用 .Find 来查找特定的数据类型?
【发布时间】:2015-08-07 02:25:19
【问题描述】:

所以当我尝试在 VBA 中编程时,我在 microsoft 网站 (https://msdn.microsoft.com/en-us/library/office/ff839746.aspx) 上发现了这个

表达式 .Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat) 表达式 表示 Range 对象的变量。

什么:要搜索的数据。可以是字符串或任何 Microsoft Excel 数据类型。

并且我希望我的代码能够在特定范围内找到第一个具有“日期”数据类型的单元格,即 temp。

Dim temp As Range, next_date As Range
temp = Range("A63:A70")
Set next_date = temp.Find(Date)
Debug.Print (next_date)

但我不断收到“未设置对象变量”错误,我认为这意味着它无法在该范围内找到日期。该范围内肯定有一个日期,但是当我将鼠标悬停在我在 .Find() 中键入的“日期”上时,我意识到它显示的是今天的日期。

我认为这段代码可能试图在该范围内查找今天的日期。但我只想让它找到一个具有通用“日期”数据类型的单元格,有没有办法在不指定特定日期的情况下做到这一点?谢谢!!

【问题讨论】:

  • 感谢您的所有回答!我意识到我误读了“可以是字符串或任何 Microsoft Excel 数据类型”——我认为这意味着 .Find 可用于查找特定数据类型,但我现在意识到这意味着 .Find 可用于查找某些字符串或数字或日期或其他excel数据类型的任何其他数据。我希望避免循环,因为我实际上正在处理数百行(但没有在示例中指定),但我可能会使用你的答案,Jeeped!还要感谢 Alex P 和 Dawid 提供的有用意见

标签: vba excel


【解决方案1】:

问题不是'A63:A70 中有日期吗?' 而是'A63:A70 中还有什么?'。日期并不是真正的单独类型的值。对于大多数意图和目的(稍后会详细介绍),它们被视为数字。如果您只想在包含日期、文本、空白但没有其他数字的范围内查找第一个日期类型,那么应该这样做。

    Dim temp As Range
    On Error Resume Next
    Set temp = Range("A63:A70").SpecialCells(xlCellTypeConstants, xlNumbers)
    If Not temp Is Nothing Then
        Set temp = temp.Cells(1, 1) 'reset temp to the 1st date if there are more than one
        Debug.Print temp.Address
    Else
        Debug.Print "no date in range"
    End If

我之所以说 大多数 意图和目的是因为 VBA 确实有一个 IsDate Function。这可能着眼于 a) 值的数字性质,b) Range.Value 和 Range.Value2 之间的差异,以及 c) 单元格的数字格式,以确定单元格值是 42,149 还是 2015 年 5 月 25 日。但是,IsDate 函数一次只能检查一个单元格,因此需要一个耗时的单元格循环。

    Dim temp As Range
    For Each temp In Range("A63:A70")
        If IsDate(temp) Then Exit For
    Next temp
    If Not temp Is Nothing Then
        Debug.Print temp.Address
    Else
        Debug.Print "no date in range"
    End If

您的示例只有 8 个单元,因此循环不会对性能造成过度损害,但如果需要单独检查几千个单元,它肯定会减慢速度。

【讨论】:

  • 关于循环性能的好点 - 我在回答中考虑到了这一点,即如果只有小范围,那么循环就可以了,但对于大型数据集,它就不会那么好了。
【解决方案2】:

temp 是一个对象Range。你必须使用set -> What does the keyword Set actually do in VBA?

我认为您无法使用.Find() 查找数据类型,但是您可以尝试查找表明我们正在处理日期的格式:

Sub tt()

Dim temp As Range, next_date As Range
Set temp = Range("A60:A70")

Application.FindFormat.NumberFormat = "m/d/yyyy"

Set next_date = temp.Find("", SearchFormat:=True)

Debug.Print next_date
Debug.Print next_date.Address

End Sub

【讨论】:

    【解决方案3】:

    我不确定您是否可以使用Find() 来查找任何日期类型的值。我认为您需要指定您正在搜索的实际日期。示例:

    Set FoundCell = Range("A1:A10").Find (what:="7/18/1998")
    

    另一种选择是简单的循环:

    Sub FindNextDate()
        Dim val As Range
    
        For Each val In Range("A1:A10")
            If IsDate(val) Then
                Debug.Print "Date: " & val & " found at cell: " & val.Address
                Exit Sub
            End If
        Next val
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多