Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 623
Default Error 91 on Find second time through a loop

I have the following code in a For loop which goes through every file in a
folder:

On Error Goto NoData
Columns("A:A").Select
Cells.Find(What:="06/30/2003").Activate

I'm having trouble getting the On Error to work. The first file where the find
fails works fine -- it goes to the NoData statement and continues on. With the
second file, I get an error 91: Object variable or With block variable not set.

How do I get the On Error statement to take effect with every file, not just the
first?

--
Regards,
Fred



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Error 91 on Find second time through a loop

If you don't have a resume statement in your error handler, then you never
end being in the error handler - then when the second error occurs, VBA
gives up on you - an error in a error handler??? - so quit. Look at the vba
help on Resume to see what you want to do.

I don't know what your error handler is doing, but you might want to just do

Columns("A:A").Select
On Error resume Next
Cells.Find(What:="06/30/2003").Activate
On Error goto 0


better would be

Dim rng as Range

set rng = Cells.Find(What:="06/30/2003")
if not rng is nothing then
rng.Activate
end if


--
Regards,
Tom Ogilvy


"Fred Smith" wrote in message
...
I have the following code in a For loop which goes through every file in a
folder:

On Error Goto NoData
Columns("A:A").Select
Cells.Find(What:="06/30/2003").Activate

I'm having trouble getting the On Error to work. The first file where the
find fails works fine -- it goes to the NoData statement and continues on.
With the second file, I get an error 91: Object variable or With block
variable not set.

How do I get the On Error statement to take effect with every file, not
just the first?

--
Regards,
Fred





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Error 91 on Find second time through a loop

Fred,
Hard to answer, as I have no idea what yor 'NoData" error handler does.
However, you can avoid the whole question and make you code better into the
bargain.

Dim FoundCell as Range
On Error Resume Next
Set FoundCell = Range("A:A").Find(What:="06/30/2003")
On Error Goto 0
If FoundCell is nothing then
'No data
else
msgbox FoundCell.Address
end if

NickHK

"Fred Smith" wrote in message
...
I have the following code in a For loop which goes through every file in a
folder:

On Error Goto NoData
Columns("A:A").Select
Cells.Find(What:="06/30/2003").Activate

I'm having trouble getting the On Error to work. The first file where the

find
fails works fine -- it goes to the NoData statement and continues on. With

the
second file, I get an error 91: Object variable or With block variable not

set.

How do I get the On Error statement to take effect with every file, not

just the
first?

--
Regards,
Fred





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 623
Default Error 91 on Find second time through a loop

Thanks for your help, Tom and Nick.

As an old Fortran programmer, I thought the GoTo part of On Error just allowed
me to skip around code I didn't want executed. I didn't know it was supposed to
be an error handling routine.

I'll definitely use your proposed code.

--
Regards,
Fred


"NickHK" wrote in message
...
Fred,
Hard to answer, as I have no idea what yor 'NoData" error handler does.
However, you can avoid the whole question and make you code better into the
bargain.

Dim FoundCell as Range
On Error Resume Next
Set FoundCell = Range("A:A").Find(What:="06/30/2003")
On Error Goto 0
If FoundCell is nothing then
'No data
else
msgbox FoundCell.Address
end if

NickHK

"Fred Smith" wrote in message
...
I have the following code in a For loop which goes through every file in a
folder:

On Error Goto NoData
Columns("A:A").Select
Cells.Find(What:="06/30/2003").Activate

I'm having trouble getting the On Error to work. The first file where the

find
fails works fine -- it goes to the NoData statement and continues on. With

the
second file, I get an error 91: Object variable or With block variable not

set.

How do I get the On Error statement to take effect with every file, not

just the
first?

--
Regards,
Fred







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run time error 1004 in loop danpt Excel Discussion (Misc queries) 4 February 11th 10 04:02 PM
run time error 1004 in loop danpt Excel Discussion (Misc queries) 1 February 10th 10 11:45 PM
Loops to find blanks then loop to find populated Bevy Excel Programming 0 June 1st 06 04:50 PM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 AM
Run-time error '91' on Cells.Find() Vera Excel Programming 0 January 20th 04 08:12 AM


All times are GMT +1. The time now is 08:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"