VBA Newbie - Runtime Error 91 using InStr

Aragorn2001

New Member
Joined
Jul 14, 2018
Messages
2
Hi all. I've gotten a lot of use out of MrExcel in the past, and I'm banging my head against a wall here. Complete VBA newbie (my last years of programming was 20+ years ago).

I'm getting the error "Object variable or With block variable not set" at the highlighted line.

Code:
Option Explicit         
Public cell As Range   


Sub superset_exercise()
Dim cellAbove, cellBelow, rangestart As Range
Dim Char, CharAbove, CharBelow As String
Dim strcell, strlengthA, strlengthB As Integer


If IsEmpty(cell) = True Then Exit Sub
[COLOR=#ff0000]strcell = InStr(1, cell, ")")             <<<runtime 91="" error<="" font="">
</runtime>[/COLOR]<runtime 91="" error<="" font="">

This sub is in the standard module, and I am trying to use this sub as a result of a worksheet change event. The Worksheet change event code is:

Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)


If Target.Cells.Count > 1 Then Exit Sub ' bail if more than one cell selected
'Application.EnableEvents = False


Set cell = Target


Application.OnKey "{ENTER}", "superset_exercise()"
'Application.EnableEvents = True


End Sub

I don't understand why I'm getting this error -- I have set the public variable cell, I made both subroutines are public, and InStr should return a number value, not an object or string.

Would someone please help? This chunk is the beginning of a larger subroutine I am having problems with.</runtime>
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try following amendments :
Code:
Option Explicit
Public cell As Range
Sub superset_exercise()
Dim cellAbove As Range, cellBelow As Range, rangestart As Range
Dim Char As String, CharAbove As String, CharBelow As String
Dim strcell As Integer, strlengthA As Integer, strlengthB As Integer


If IsEmpty(cell) = True Then Exit Sub
strcell = InStr(1, cell, ")")
End Sub
Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub ' bail if more than one cell selected
'Application.EnableEvents = False
Set cell = Target
Call superset_exercise
'Application.EnableEvents = True
End Sub
This might also help : https://www.houselogic.com/organize-maintain/diy-repair/repair-walls-give-rooms-fresh-face/
 
Upvote 0
Hi footoo, thanks for replying. I made all the changes but still get the same error on the same line.

Thanks for the home and garden link, I'll probably need some new dry wall by the time I figure this out :laugh:
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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