如果找不到日期,请停止宏

时间:2016-03-13 17:23:48

标签: excel excel-vba macros vba

我需要在列中搜索日期,如果没有找到则停止宏。日期来自Cell

Dim rng1 As Range
Dim strSearch As String
strSearch = Worksheets("Calculations").Cells(4, 3).Value
MyInput.Activate
Set rng1 = Range("H:H").Find(strSearch, , xlValues, xlWhole)
If Not rng1 Is Nothing Then
'Macro follows
Else
    MsgBox "Date not found"
End If
End Sub

即使日期存在,此宏的作用始终会返回消息框。

由于

1 个答案:

答案 0 :(得分:0)

您正在尝试以字符串类型查找日期。 我刚刚用日期类型将strSearch更改为dteSearch。

Dim rng1 As Range
Dim dteSearch As Date
dteSearch = Worksheets("Calculations").Cells(4, 3).Value
MyInput.Activate
Set rng1 = Range("H:H").Find(dteSearch, , xlValues, xlWhole)
If Not rng1 Is Nothing Then
'Macro follows
Else
    MsgBox "Date not found"
End If
End Sub
祝你好运!