Problems with Objects

Debbs76

New Member
Joined
Nov 26, 2009
Messages
22
Hi,

Can somebody please explain to me why I am getting the object variable not set error when I am doing the following code?

Sub HideMonths()
'
' HideMonths Macro
'
Dim myFind As Range
'
Set myFind = Rows(1).Find(What:="1/5/2007", _
After:=Cells(1), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
Range("C1", myFind.Offset(0, -1)).EntireColumn.Hidden = True
End Sub

I have copied this straight out of the Step by Step Office Excel 2007 Visual Basics for Applications book and no matter how many times I try it I get the same error and it's driving me mad. :mad:

Help would be very much appreciated - thx

The code is supposed to hide everything from Column C to the column before the date in the find (in this case column H)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hello Debbs76,

The result of the Find method will either be the cell where the value was found or the special object value Nothing. You need to test the result of the Find after it is executed to decide what action should be taken next.
Rich (BB code):
Sub HideMonths()
'
' HideMonths Macro
'
Dim myFind As Range
'
Set myFind = Rows(1).Find(What:="1/5/2007", _
After:=Cells(1), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
  If Not myfind Is Nothing Then
     Range("C1", myFind.Offset(0, -1)).EntireColumn.Hidden = True
  Else
    MsgBox What & "Not Found."
End Sub

Sincerely,
Leith Ross
 
Upvote 0
Hi,

1.Try change from
Lookin=xFormulas
to
Lookin=xlValues

2.If this doesnt work check if your dates in row 1 are text or dates.

If they are dates try
Find(What:=CDate("1/5/2007"), ...


HTH

M.

 
Upvote 0
Hi both for getting back so quickly. The result of the find is giving me 'nothing' as the value. But the book seems to think that this code will result in the columns being hidden. I'm in Australia. Would this affect the result of the find - as in is there another format Excel would recognise?? If I do a find in excel it works no problem but when I'm looking in the code it returns 'nothing' and therefore doesn't work. I'm just frustrated to be reading through the book and have errors. :(

The next line of code does deal with the code returning an empty value but it should be working which is why I jumped on here to ask.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top