Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a spreadsheet that has a rolling date in Column B and it always opens, unsurprisingly, at the top.

I've put the following code into ThisWorkbook but I am getting an error on the Set line ...

Run-time error '91';

Object variable or With block variable not set


Can somebody please tell me what I'm doing wrong ?!?

VB
Private Sub Workbook_Open()

Dim myDate As Date
Dim myCell As Range

myDate = Date

With Worksheets("Sheet1")
    Set myCell = Columns(2).Find(What:=myDate, LookIn:=xlValues).Activate
End With

Application.Goto Selection, True


End Sub
Posted

1 solution

The problem is that the Find has failed to find the date, so the .Activate is being issued against a null object.

Try checking the result first e.g.
VB
With Worksheets("Sheet1")
    Set myCell = Columns(2).Find(What:=myDate, LookIn:=xlValues)
    If Not myCell Is Nothing Then
        myCell.Activate
    End If
End With
 
Share this answer
 
Comments
Gary Heath 3-Jul-14 6:39am    
Brilliant, thank you, I do love the clear and obvious Error Messages Microsoft display !!!
CHill60 3-Jul-14 7:09am    
:laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900