Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Run-time error ‘91’:


I made this code as simple as possible to get to the bottom of th
problem. Now instead of failing some of the tine, it dies all th
time. Any insight please?

Sub Macro1()
'
For i = 1 To 1000
Columns("A:A").Select
FoundIt = Selection.Find(What:=i, _

After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

i = i + 1

Next

End Su

--
GC
-----------------------------------------------------------------------
GCF's Profile: http://www.excelforum.com/member.php...nfo&userid=412
View this thread: http://www.excelforum.com/showthread.php?threadid=26694

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Run-time error ‘91’:


The problem is when the Find function doesn't find anything. If nothin
is found, the error is generated. The way around it is to use an erro
handler:

Sub Macro1()
Dim i As Integer
Dim FoundIt

For i = 1 To 1000
Columns("A:A").Select
ON ERROR RESUME NEX
FoundIt = Selection.Find(What:=i, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

[b]'Here you can check for an error and do what you need to do
If Err.Number < 0 Then
'Do something
End If
i = i + 1

Next

End Sub



--
kkkni
-----------------------------------------------------------------------
kkknie's Profile: http://www.excelforum.com/member.php...nfo&userid=754
View this thread: http://www.excelforum.com/showthread.php?threadid=26694

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Run-time error '91':

An alternative to using an error handler is - don't try to select nothing.

Sub Macro1()
Dim FoundIt as Range
For i = 1 To 1000
set FoundIt = Columns("A:A").Find(What:=i, _
After:=Range("A65536"), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
if not FoundIt is nothing then
' operate on Foundit
' msgbox Foundit.Address
'
' Foundit.Select
Else
' target not found
end if
i = i + 1
Next

End Sub

If you want to progressively search down column A, then change
After:=Range("A65536") back to After:=ActiveCell and put
Range("A65536").Select at the top of the code.

--
Regards,
Tom Ogilvy


"GCF" wrote in message
...

I made this code as simple as possible to get to the bottom of the
problem. Now instead of failing some of the tine, it dies all the
time. Any insight please?

Sub Macro1()
'
For i = 1 To 1000
Columns("A:A").Select
FoundIt = Selection.Find(What:=i, _

After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

i = i + 1

Next

End Sub


--
GCF
------------------------------------------------------------------------
GCF's Profile:

http://www.excelforum.com/member.php...fo&userid=4124
View this thread: http://www.excelforum.com/showthread...hreadid=266948



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
Visual Basic Error Run Time Error, Type Mismatch Meg Partridge Excel Discussion (Misc queries) 12 September 10th 08 06:10 PM
Run time error 1004, General ODBC error [email protected] New Users to Excel 0 September 19th 05 01:41 AM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 AM
Code Error - Run Time Error 5 (Disable Cut, Copy & Paste) Tim[_36_] Excel Programming 4 April 23rd 04 02:53 AM
Run-time error '11' & Run-time error '1004' Piers Clinton-Tarestad Excel Programming 0 January 9th 04 07:45 PM


All times are GMT +1. The time now is 09:55 AM.

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

About Us

"It's about Microsoft Excel"