我有一个工作表,其中“A”到“I”列填充了数据。a 列包含从 30/11/2011 到 6/12/2011 的日期。我有一个用户表单,其中有 2 个选项按钮。选择第一个时,将使用所有日期。When the second is selected 6 textboxes that can be used to enter a start- and an enddate. 这些日期用于创建包含所有数据/所选间隔的范围。我曾经.find
制定一个选定的开始日期和结束日期的范围。
我为此编写了一个代码,它在一个单独的模块中工作,但我无法让它在用户表单中工作,因为range.Find
返回“无”。我花了一段时间才让它在模块中工作,因为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 返回“无”
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 已正确定义,代码只是无法返回找到日期的单元格并给我一个运行时错误 1004:方法'范围对象'_global'失败,因为我试图设置rngdates 范围从无到无。
编辑:知道当我在选择第一个选项按钮的情况下运行整个代码时,我可以随后使用单独的模块来创建范围RngDates
,但是,当我使用第二个选择运行代码并在它崩溃后停止它时,这可能会有所帮助,单独的模块也找不到所需的单元格。
编辑:澄清它是关于 2011 年 12 月 2 日至 2011 年 12 月 4 日的日期,而不是 2 月。
提前致谢