【发布时间】:2019-10-10 11:46:06
【问题描述】:
我在使用 .Find(What:-.
时收到错误 91“对象变量或未设置块变量”我想通过从“dailysheet”中搜索 Cells(2,2) 中的值来找到“overview”表中的列索引号。
我在 lnCol = 行上得到错误。我认为这与格式化或设置“checkdate”变量有关。
任何帮助将不胜感激!
Sub checkingdate_Click()
Dim overview As Worksheet
Dim dailysheet As Worksheet
Dim datecheck As Range
Dim checkdate As Date
Dim lnRow As Long
Dim lnCol As Long
Set overview = ThisWorkbook.Worksheets("overview")
Set dailysheet = ThisWorkbook.Worksheets("dailysheet")
Set datecheck = dailysheet.Cells(2, 2)
lnRow = 5
overview.Rows("5").EntireRow.Hidden = False 'Adjust potentially
With datecheck
.NumberFormat = "dd/mm/yyyy"
'.NumberFormat = "@"
End With
With overview.Rows("5")
.NumberFormat = "dd/mm/yyyy"
'.NumberFormat = "@"
End With
checkdate = dailysheet.Cells(2, 2).Value
MsgBox datecheck.Value
MsgBox checkdate
lnCol = overview.Cells(lnRow, 1).EntireRow.Find(What:=checkdate, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
MsgBox lnCol
'=====
' END
'=====
overview.Rows("5").EntireRow.Hidden = True 'Adjust potentially
With overview.Rows("5")
.NumberFormat = "dd"
End With
With overview.Columns("B:ABO")
.ColumnWidth = 4.57
End With
End Sub
编辑:通过将变量 checkdate 更改为 dailysheet.Cells(2,2).Formula 而不是 .Values 并更改为 LookIn:=xlFormulas 而不是 LookIn:=xlValues 找到了解决方案。我还更改了overview.Rows("5")的.NumberFormat,使日期变成了序列号,从而可以从“checkdate”的序列号中搜索。
修改后的部分代码如下:
With overview.Rows("5")
'.NumberFormat = "dd/mm/yyyy"
.NumberFormat = "@"
End With
checkdate = dailysheet.Cells(2, 2).Formula
MsgBox datecheck.Value
MsgBox checkdate
lnCol = overview.Cells(lnRow, 1).EntireRow.Find(What:=checkdate, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
【问题讨论】:
-
checkdate 到达此行时的值是多少?该日期是否存在于您正在查看的行中?
-
嗨@Dave 这是 09/01/2020 - 这是我正在测试的正确日期 - 类型:日期
-
你会在这里找到类似的东西 stackoverflow.com/questions/11917841/… 如果你确定你的日期存在于你正在搜索的行中,请仔细检查你的本地日期格式是否没有切换天/月
-
@Dave 我认为这可能是问题所在,但我不确定如何解决它...
-
改用
LookIn:=xlFormulas。