使用.Find(What:-

时间:2019-10-10 11:46:06

标签: excel vba

使用.Find(What:-。

时,我收到错误91“对象变量或未设置块变量”。

我想通过从“每日工作表”中搜索Cells(2,2)中的值来在“概述”工作表中找到列索引号。

我在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

1 个答案:

答案 0 :(得分:0)

通过将变量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