Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default VBA: Object variable or With block variable not set (Error 91) - ???

I am executing the following statement in VBA:

SetPtr = "Part 1"
Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

And I get the following message:
Runtime Error 91:
Object Variable or With Block Variable not set.

What does this mean?

Thanks,
Mac Lingo
Berkeley, CA


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA: Object variable or With block variable not set (Error 91) - ???

If "Part 1" wasn't found, then you're trying to activate something that doesn't
exist--and it blows up.

The more usual approach:

dim FoundCell as range
SetPtr = "Part 1"
set foundcell = Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)

if foundcell is nothing then
msgbox "Not found"
else
foundcell.activate
end if



Mac Lingo wrote:

I am executing the following statement in VBA:

SetPtr = "Part 1"
Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

And I get the following message:
Runtime Error 91:
Object Variable or With Block Variable not set.

What does this mean?

Thanks,
Mac Lingo
Berkeley, CA


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default VBA: Object variable or With block variable not set (Error 91)- ???

It means that the text "Part 1" was not found. You can avoid this error
by setting a range object when the value is found eg:

Dim SetPtr As String
Dim fndRng As Range
SetPtr = "Part 1"
Set fndRng = Cells.Find(What:=SetPtr)
If Not fndRng Is Nothing Then
fndRng.Select 'or whatever
End If

Hope this helps
Rowan

Mac Lingo wrote:
I am executing the following statement in VBA:

SetPtr = "Part 1"
Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

And I get the following message:
Runtime Error 91:
Object Variable or With Block Variable not set.

What does this mean?

Thanks,
Mac Lingo
Berkeley, CA


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Object variable or With block variable not set (Error 91) - ???

Hi Mac Lingo,

The find method returns a range object which needs to be set.

Try something like:

Dim SetPtr As String
Dim rng As Range

SetPtr = "Part 1"

Set rng = Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
'Your code
End If

---
Regards,
Norman



"Mac Lingo" wrote in message
m...
I am executing the following statement in VBA:

SetPtr = "Part 1"
Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

And I get the following message:
Runtime Error 91:
Object Variable or With Block Variable not set.

What does this mean?

Thanks,
Mac Lingo
Berkeley, CA



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Object variable or With block variable not set (Error 91) - ???

The find method returns a range object which needs to be set.

Should read:

The find method returns a range object which needs to be set to a range
variable which can be tested; or else you need to use an error handler to
prevent the error which results if the search string is not located.

The code was fine, the explanation was prematurely truncated!

---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Mac Lingo,

The find method returns a range object which needs to be set.

Try something like:

Dim SetPtr As String
Dim rng As Range

SetPtr = "Part 1"

Set rng = Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rng Is Nothing Then
'Your code
End If

---
Regards,
Norman



"Mac Lingo" wrote in message
m...
I am executing the following statement in VBA:

SetPtr = "Part 1"
Cells.Find(What:=SetPtr, _
After:=ActiveCell, _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate

And I get the following message:
Runtime Error 91:
Object Variable or With Block Variable not set.

What does this mean?

Thanks,
Mac Lingo
Berkeley, CA







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
Runtime Error '91' Object variable or With block variable not set Alec Coliver Excel Discussion (Misc queries) 2 October 24th 09 02:29 PM
Run-time error '91': "Object variable or With block variable not set Mike[_92_] Excel Programming 2 December 30th 04 10:59 AM
Error 91 -- Object variable or With block variable not set Rachael[_3_] Excel Programming 2 November 22nd 04 02:59 PM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM
Error 91 - Object variable with block variable not set Jim[_35_] Excel Programming 2 November 27th 03 03:34 AM


All times are GMT +1. The time now is 01:25 PM.

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"