I am receiving an Error 91 "Object variable or With block variable not set" when using .Find(What:-.
I want to find the column index number in the "overview" sheet by seaching for the value in Cells(2,2) from the "dailysheet".
I get the error on lnCol = line. I think it something to do with the formatting or setting the "checkdate" variable.
Any help would be great appreciated!
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
EDIT: Found a solution by changing the variable checkdate to dailysheet.Cells(2,2).Formula rather than .Values and changed to LookIn:=xlFormulas rather than LookIn:=xlValues. I also changed the .NumberFormat of overview.Rows("5"), so that the dates became serial numbers, thereby becoming searchable from the serial number from "checkdate".
Edited portion of the code is below:
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