ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA: Object variable or With block variable not set (Error 91) - ??? (https://www.excelbanter.com/excel-programming/344427-vba-object-variable-block-variable-not-set-error-91-a.html)

Mac Lingo[_2_]

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



Dave Peterson

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

Rowan Drummond[_3_]

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



Norman Jones

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




Norman Jones

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







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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com