Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Cells.Find woe

I've been trying to get the following code to work in Excel 2003. It's fine
if X is found. It breaks on the X assignment statement if Cells.Find fails to
find anything.

The error is *always*: Object variable or With block variable not set
(Error 91)

I've tried it with and without using the "set" in front of the X assignment,
with and without declaring DIM X, alternately as Object and as Range

Range("A1").Select
x = Cells.Find(What:=",", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate


If (x Is Nothing) Then
MsgBox "Not Found"
Else
MsgBox "x is " & x
End If


So, while it works okay with the "On Error Resume Next" statement inserted,
I must be doing something wrong for it to generate a stop execution
error...I'd like to find out what is the proper way of doing this. Can anyone
give me a clue?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Cells.Find woe

Hi Jess,

Try:

'=============
Public Sub Tester()
Dim Rng As Range
Const strSearch As String = "abc"

Set Rng = Cells.Find(What:=strSearch, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

If Not Rng Is Nothing Then
MsgBox strSearch & " found at " & Rng.Address(0, 0)
Else
MsgBox strSearch & " not found"
End If

End Sub
'<<=============


---
Regards,
Norman


"Jess Wundring" wrote in message
...
I've been trying to get the following code to work in Excel 2003. It's
fine
if X is found. It breaks on the X assignment statement if Cells.Find fails
to
find anything.

The error is *always*: Object variable or With block variable not set
(Error 91)

I've tried it with and without using the "set" in front of the X
assignment,
with and without declaring DIM X, alternately as Object and as Range

Range("A1").Select
x = Cells.Find(What:=",", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate


If (x Is Nothing) Then
MsgBox "Not Found"
Else
MsgBox "x is " & x
End If


So, while it works okay with the "On Error Resume Next" statement
inserted,
I must be doing something wrong for it to generate a stop execution
error...I'd like to find out what is the proper way of doing this. Can
anyone
give me a clue?

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Cells.Find woe

One way:

Dim x As Range
Set x = Cells.Find( _
What:=",", _
After:=Range("A1"), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not x Is Nothing Then
MsgBox "x is " & x.Value
Else
MsgBox "Not Found"
End If

The problem you're having is that you're trying to activate a
non-existent range.


In article ,
"Jess Wundring" wrote:

I've been trying to get the following code to work in Excel 2003. It's fine
if X is found. It breaks on the X assignment statement if Cells.Find fails to
find anything.

The error is *always*: Object variable or With block variable not set
(Error 91)

I've tried it with and without using the "set" in front of the X assignment,
with and without declaring DIM X, alternately as Object and as Range

Range("A1").Select
x = Cells.Find(What:=",", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate


If (x Is Nothing) Then
MsgBox "Not Found"
Else
MsgBox "x is " & x
End If


So, while it works okay with the "On Error Resume Next" statement inserted,
I must be doing something wrong for it to generate a stop execution
error...I'd like to find out what is the proper way of doing this. Can anyone
give me a clue?

Thanks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Cells.Find woe

Thanks Norm and JE but I've tried it with

Dim x as Object
and
Dim x as Range
and without any dim statement whatsoever

I've tried it for each of the above cases with

Set x = Cells.Find....
and just plain old
x = Cells.Find....

all of these cases give me error 91 when the Cells.Find statement fails to
find an instance of "What".

Am I missing something? The only diff I see in Norm's example is that he
assigns the What to a string var. And I see no diff in JE's example, unless
you think it might be my variable names?

Anyway....Thanks for responding you guys. I really appreciate it. Hope
Microsoft fixes their Help system for Office 12. It's so f****g broken right
now I want to scream whenever I try to use it. Sometimes I actually do
scream. :)


"Jess Wundring" wrote:

I've been trying to get the following code to work in Excel 2003. It's fine
if X is found. It breaks on the X assignment statement if Cells.Find fails to
find anything.

The error is *always*: Object variable or With block variable not set
(Error 91)

I've tried it with and without using the "set" in front of the X assignment,
with and without declaring DIM X, alternately as Object and as Range

Range("A1").Select
x = Cells.Find(What:=",", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate


If (x Is Nothing) Then
MsgBox "Not Found"
Else
MsgBox "x is " & x
End If


So, while it works okay with the "On Error Resume Next" statement inserted,
I must be doing something wrong for it to generate a stop execution
error...I'd like to find out what is the proper way of doing this. Can anyone
give me a clue?

Thanks

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Cells.Find woe

AHA! I just didn't see it.

I needed to eliminate the .Activate method on the Cells.Find......

THANK YOU BOTH (especially JE) for pointing this out to me.

You're awesome!



"Jess Wundring" wrote:

I've been trying to get the following code to work in Excel 2003. It's fine
if X is found. It breaks on the X assignment statement if Cells.Find fails to
find anything.

The error is *always*: Object variable or With block variable not set
(Error 91)

I've tried it with and without using the "set" in front of the X assignment,
with and without declaring DIM X, alternately as Object and as Range

Range("A1").Select
x = Cells.Find(What:=",", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,
MatchCase:=False _
, SearchFormat:=False).Activate


If (x Is Nothing) Then
MsgBox "Not Found"
Else
MsgBox "x is " & x
End If


So, while it works okay with the "On Error Resume Next" statement inserted,
I must be doing something wrong for it to generate a stop execution
error...I'd like to find out what is the proper way of doing this. Can anyone
give me a clue?

Thanks

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
Need Cells.find to find first number in a row which is 8000 Kasper Excel Discussion (Misc queries) 9 December 15th 08 02:10 PM
how to find cells that refer to data in other cells in excel Aman Excel Discussion (Misc queries) 8 December 2nd 07 10:02 PM
from a group of cells.find average of cells containing values farm Excel Discussion (Misc queries) 1 December 21st 06 08:50 PM
How to find multiple cells/replace whole cells w/data dcurylo Excel Discussion (Misc queries) 2 November 30th 05 08:06 PM
If Cells.Find can't find anything achidsey Excel Programming 2 November 3rd 05 06:50 PM


All times are GMT +1. The time now is 10:23 PM.

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"