VBA 在用户表单中找到 returns "nothing"

VBA find returns "nothing" in userform

我有一个工作表,其中 "A" 到 "I" 列填满了数据。 a 列包含从 2011 年 11 月 30 日到 2011 年 6 月 12 日的日期。我有一个用户表单,其中有 2 个选项按钮。选择第一个时,将使用所有日期。 When the second is selected 6 textboxes that can be used to enter a start- and an enddate.这些日期用于创建具有所有 data/the 个选定间隔的范围。我使用 .find 来确定所选开始日期和结束日期的范围。

我为此编写了一个代码,它在一个单独的模块中工作,但我无法让它在用户窗体中工作,因为 range.Find returns "nothing." 我花了一段时间让它在模块中工作,因为 range.find 很难与日期结合使用,但现在它可以工作了,我不知道为什么它在用户窗体中不起作用。

我广泛搜索了论坛,但找不到任何对我有帮助的东西。我希望这只是一个打字错误,但我真的找不到为什么它不起作用。

这是模块中的代码:

sub Find()
Dim Dates As Range   
Dim Data As Range
Dim LastRow As Long
Dim LastCol As Long

Dim RngStart As Range 
Dim RngEnd As Range
Dim RngDates As Range
Dim DateStart As String
Dim DateEnd As String

Dim TextboxDate1 As Long   'these variables represent the textboxvalues of the userform
Dim TextboxDate2 As Long
Dim TextboxMonth1 As Long
Dim TextboxMonth2 As Long
Dim TextboxYear1 As Long
Dim TextboxYear2 As Long
    TextboxDate1 = 2
    TextboxDate2 = 4
    TextboxMonth1 = 12
    TextboxMonth2 = 12
    TextboxYear1 = 2011
    TextboxYear2 = 2011


    ThisWorkbook.Worksheets("blad1").Activate
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        Set Data = Range(Cells(1, 2), Cells(LastRow, LastCol))
        Set Dates = Range(Cells(1, 1), Cells(LastRow, 1))

DateStart = TextboxMonth1 & "/" & TextboxDate1 & "/" & TextboxYear1 '"12/2/2011"
DateEnd = TextboxMonth2 & "/" & TextboxDate2 & "/" & TextboxYear2 '"12/4/2011"
    Set RngStart = ThisWorkbook.Worksheets("blad1").Columns("A").find(DateStart)
    Set RngEnd = Columns("a").find(what:=DateEnd, after:=Cells(1, 1), searchdirection:=xlPrevious)
    Set RngDates = Range(RngStart, RngEnd)
        MsgBox RngDates.Address 'should return A160:A447
End Sub

然而,当我尝试在用户表单中 运行 这段代码时,.find returns "nothing"

Dim Dates As Range
Dim Data As Range
Dim LastRow As Long
Dim LastCol As Long
Dim RngStart As Range
Dim RngEnd As Range
Dim RngDates As Range
Dim DateStart As String
Dim DateEnd As String

ThisWorkbook.Worksheets("blad1").Activate

    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
        Set Data = Range(Cells(1, 2), Cells(LastRow, LastCol))
        Set Dates = Range(Cells(1, 1), Cells(LastRow, 1))

If OptionButton1.Value = False And OptionButton2.Value = False Then
    MsgBox "specify time domain"
End If

If OptionButton1.Value = True Then
    Set RngDates = ThisWorkbook.Worksheets("blad1").Range(Cells(2, 1), Cells(LastRow, 1))
End If

If OptionButton2.Value = True Then
    DateStart = TextboxMonth1 & "/" & TextboxDate1 & "/" & TextboxYear1 '"12/2/2011"
    DateEnd = TextboxMonth2 & "/" & TextboxDate2 & "/" & TextboxYear2 '"12/4/2011"
        Set RngStart = ThisWorkbook.Worksheets("blad1").Columns("A").find(DateStart)
        Set RngEnd = Columns("a").find(what:=DateEnd, after:=Cells(1, 1), searchdirection:=xlPrevious)
        Set RngDates = Range(RngStart, RngEnd)
            MsgBox RngDates.Address 'should return A160:A447
End If

我可以看到 DateStart 和 DateEnd 已正确定义,代码无法 return 找到日期的单元格并给我一个 运行time error 1004: method 'range of object '_global' 失败,因为我试图将 rngdates 设置为从无到无的范围。

编辑:知道当我 运行 选择第一个选项按钮的整个代码后我可以使用单独的模块创建范围 RngDates 但是,当我 运行 使用第二个选择的代码,我在它崩溃后停止了它,单独的模块也找不到所需的单元格。

编辑:澄清一下日期是 2011 年 12 月 2 日到 2011 年 12 月 4 日,而不是 2 月。

提前致谢

使用.Find设置RngStart时指定工作簿和工作表,使用.Find设置RngEnd时不指定工作簿和工作表工作表。

这可能会导致查找失败。

我在工作表“Sheet1”的 A 列中填写了一些日期。

我使用以下控件创建了一个表单:

  • 文本框:txtDate
  • 命令按钮:cmdFind
  • 标签:lblResult

这是表单中的代码:

Private Sub cmdFind_Click()

  Dim Rng As Range
  Dim DateDat As Date

  With Worksheets("Sheet1")

    DateDat = CDate(txtDate)

    Set Rng = .Columns("A").Find(What:=DateDat)

    If Rng Is Nothing Then
      lblResult.Caption = Format(DateDat, "d mmm yyyy") & " not found"
    Else
      lblResult.Caption = Format(DateDat, "d mmm yyyy") & " found in " & _
                          Replace(Rng.Address, "$", "")
    End If

  End With

End Sub

每次我在文本框中输入日期并单击按钮时,查找的结果都会显示在标签中。 CDate 对什么构成日期有点挑剔,但除此之外我用这段代码很容易找到日期。

注意Find语句中的变量类型为Date.