I'm no expert in VBA (currently teaching myself) and I was hoping someone could help with an error I'm facing. I'm currently working on a macro to go to today's date that is located somewhere in one of the worksheets that function as weekly timesheets. Each timesheet is based on the same template. I essentially want to loop through each worksheet, search for todays date in column B and, if it's found, select the cell to the left of it and stop searching through the other sheets.
The dates in this column are based on formulas so I can't used find. I think I'm nearly there but I'm facing a "runtime error 91: Object variable or With block variable not set." Below is my code. I'm getting the error on the targetcell = ws.Cells(Counter, 2)
line.
Sub test()
Dim ws As Worksheet
Dim FindString As Date ' Todays date
Dim DateRange As Range ' range of dates on aparticular timesheet
Dim target As Long ' date currently being checked
Dim targetcell As Range ' location of date being checked
Dim found As Boolean ' checks for date found
Dim LR As Double ' Last row in the column storing dates
'convert today's date into a string that can be searched for in the date range
FindString = CLng(Date)
found = False 'starting point for found
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Dashboard" And ws.Name <> "Template" Then 'only checks timesheets
ws.Activate ' Select the timesheet
LR = ws.Range("B:B").SpecialCells(xlCellTypeLastCell).Row
For Counter = 1 To LR
targetcell = ws.Cells(Counter, 2)
target = CLng(targetcell)
If target = FindString Then
ws.Cells(Counter, 1).Select
found = True
Exit For
End If
Next Counter
If found = True Then
Exit For
End If
End If
Next ws
End Sub
I have tried to get rid of targetcell = ws.Cells(Counter, 2)
all together by changing target = CLng(targetcell)
to target = CLng(ws.Cells(Counter, 2))
but then I get runtime error 13: type mismatch on that line.
Any help would be greatly appriciated!