VBA,查找功能找不到输入

时间:2020-08-10 15:40:04

标签: vba find

我有日期格式为dd.mm.yyyy的单元格,只想保留行(其中A列包含日期,并在输入框中输入,其余应删除)。

我只是想不通,为什么find函数找不到输入。我尝试使用确切的日期而不是AddDate,但是它不起作用,所以我猜问题出在格式上?

请帮助。这是代码的一部分:

Dim AddDate As String

AddDate = InputBox("Enter date for export.", "Enter date", "DD.MM.YYYY")
          
Dim FoundRange As Range

Set FoundRange = Range("A:A").Find(what:=AddDate, LookIn:=xlValue, lookat:=xlWhole)
   If FoundRange Is Nothing Then
   MsgBox "Date not found. Use the correct format DD.MM.YYYY."
   Workbooks("KOREN.xlsm").Close
   GoTo The_End
Else

Dim i As Long
   Application.EnableEvents = False
   With Worksheets("KOREN")
      For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 3 Step -1
        Select Case LCase(.Cells(i, 1).Value)
            Case AddDate
                'do nothing
            Case Else
                .Rows(i).EntireRow.Delete
        End Select
      Next i
   End With
Application.EnableEvents = True
End If

1 个答案:

答案 0 :(得分:0)

最后,明白了。

我添加了

If Not Format(AddDate, "DD.MM.YYYY") = AddDate Then
        MsgBox "Wrong format. Use DD.MM.YYYY."
        GoTo ReEnter

输入框并设置FoundRange以查找DateValue

set FoundRange = Range("A:A").Find(what:=DateValue(AddDate), LookIn:=xlFormulas, lookat:=xlWhole)