excel vba range.find找不到日期

标签 excel date find runtime-error vba

我正在尝试建立最小日期及其相关行以供以后在子例程中使用。

我遇到了一个错误,过去几个小时我一直在隔离问题并寻找解决方案,但无济于事。

我有一个电子表格,其中 column B包含按顺序排列的日期范围。我可以找到最小日期(消息框正确返回 2015 年 11 月 14 日),但是当我尝试使用该日期值来识别行号时,我得到错误 91。这是我的代码:

    Sub testing()

    ThisWorkbook.Worksheets("Burn Curve Data").Activate

    Dim rDateColumn As Range
    Dim dMinDate As Date
        Set rDateColumn = ActiveSheet.Range("B:B")
        dMinDate = Application.WorksheetFunction.Min(rDateColumn)

    MsgBox dMinDate

    Dim rMinCell As Range
    Dim intMinRow As Integer
        Set rMinCell = rDateColumn.Find(dMinDate, LookIn:=xlValues, LookAt:=xlWhole)
        intMinRow = rMinCell.Row

    MsgBox intMinRow

    End Sub

我尝试插入 If Not Set rMinCell 之后的声明并确定range.find没有找到日期。这是我用来识别错误的语句,但我删除了它以清理此帖子的代码。
    If Not rMinCell is Nothing Then
        intMinRow = rMinCell.Row
    Else
        MsgBox "error finding rMinCell"
    End If

我也试过重新保存dMinDate转换成一个字符串,然后在 range.find 中使用该字符串但我遇到了同样的错误,找不到日期。

另一个可能相关或不相关的细微差别是此数据存在于工作表上的指定范围内。我的 range.find 做错了什么线?!?

最佳答案

正如评论中所讨论的,当列的格式不是 Date 时,就会出现问题。 .

要通过代码执行此操作,您可以在设置 rDateColumn 后添加此行多变的:

rDateColumn.NumberFormat = "m/d/yyyy"

这应该将列格式化为日期和您的 Find方法应该正常工作。

或者,您可以选择列,单击“设置单元格格式”,然后确保在“类别”下选择“日期”。

关于excel vba range.find找不到日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32054114/

相关文章:

PHP excel 在一个单元格中

java - 比较时间戳与实际时间(Java)

ubuntu - 在 Linux 终端中使用 find 和脚本提取数据

jquery - jquery中查找和过滤的区别

linux - 将目录下所有文件的第5列除以3?

excel - VBA Excel : call a macro on close but not from the Workbook_BeforeClose function?

vba - 如果条目与另一个表重复,则从表中删除行

c# - 将 DataGrid 导出到 CSV 或 Excel

MySQL : group records by datetime ignoring the "seconds"

R,strptime(),%b,尝试将字符转换为日期格式