Runtime error 91

Pobek

Board Regular
Joined
Jul 7, 2015
Messages
89
I have a model that performs an automatic hide of a formular bar depending on what cells you pick (password entering cells and so on). There is a function that does this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("Tauthcod")) Is Nothing Then
Application.DisplayFormulaBar = False
Else
Application.DisplayFormulaBar = True
End If
End If
End Sub

The model works and no problem. However when I send the file to someone else and that person tries to enable editing, i get the runtime error 91 saying "Object Variable or With block variable not set". When you debug, the second line (1st IF statement) gets highlighted.

Any quick and permanent fixes? ... silver-bullet anyone?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I am not sure, but typically, the first line would look like this:
Code:
[COLOR=#333333]If Target.Count = 1 Then
instead of this:
[/COLOR]
Code:
If Selection.Count = 1 Then[/COLOR]

One typically uses "Target" and not "Selection" in Event Procedure code.
 
Upvote 0
Thanks Joe, that seems to have worked.

Can you briefly explain why that is please?
 
Upvote 0
By definition, Target is the range that was selected that triggers the Worksheet_SelectionChange event procedure code to run.
In Worksheet_Change event procedure code, Target is the range that was changed that triggers it to run.

Here is a good write-up on Event Procedures. Chip gets into Target a bit in the "Sample Event Procedure" section.
http://www.cpearson.com/excel/events.aspx
 
Upvote 0
By definition, Target is the range that was selected that triggers the Worksheet_SelectionChange event procedure code to run.
In Worksheet_Change event procedure code, Target is the range that was changed that triggers it to run.

Here is a good write-up on Event Procedures. Chip gets into Target a bit in the "Sample Event Procedure" section.
http://www.cpearson.com/excel/events.aspx



Thanks for that!! Ill have a look!
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top