Solved

Get rid of Run-Time Error '91'

Posted on 2014-12-22
3
219 Views
Last Modified: 2014-12-22
I keep receiving this error message, "Run-Time Error '91':Object variable or With block variable not set",  when ever I click on any cell outside of column A. It is very annoying and distracting. How can I get rid of this message?
Below is the code I am using

Should I get rid of the 'intersect' line?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("$A$12:$A$1500")) Then

    If Range("C24").Value = 0 Then
        Rows("24:27").EntireRow.Hidden = True
    Else
        Rows("24:27").EntireRow.Hidden = False
    End If
    
    If Range("C23").Value = 0 Then
        Rows("23").EntireRow.Hidden = True
    Else
        Rows("23").EntireRow.Hidden = False
    End If
    
    If Range("C31").Value = 0 And Range("C32").Value = 0 Then
            'both ranges are zero, hide rows 30-35
            Rows("30:37").EntireRow.Hidden = True
        ElseIf Range("C32").Value > 0 Then
            'range > 0, display rows 32-35
            Rows("32:37").EntireRow.Hidden = False
            Rows("30").EntireRow.Hidden = False
            'hide row 31
            Rows("31").EntireRow.Hidden = True
        ElseIf Range("C31").Value > 0 Then
            'range > 0, display row 31
            Rows("30:31").EntireRow.Hidden = False
            'hide rows 32-35
            Rows("32:35").EntireRow.Hidden = True
        Else
            'display rows 30-35
            Rows("30:35").EntireRow.Hidden = False
        End If
        
    If Range("C39").Value = 0 And Range("C40").Value = 0 Then
            Rows("38:45").EntireRow.Hidden = True
        ElseIf Range("C39").Value > 0 Then
            Rows("40:43").EntireRow.Hidden = True
            Rows("38:39").EntireRow.Hidden = False
        ElseIf Range("C40").Value > 0 Then
            Rows("40:43").EntireRow.Hidden = False
            Rows("39").EntireRow.Hidden = True
        Else
            Rows("38:45").EntireRow.Hidden = False
        End If
End If

End Sub

Open in new window

0
Comment
Question by:Boston617
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40514026
Try testing the returned range for Nothing. This example uses a variable to capture the Range.
Dim rngIntersect As Range
Set rngIntersect = Intersect(Target, Range("$A$12:$A$1500"))

If Not rngIntersect Is Nothing Then

Open in new window

0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40514037
I think the problem is that Intersect is returning a range, not a boolean. Try this instead

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Intersect(Target, Range("$A$12:$A$1500")) Is Nothing Then

Open in new window

0
 

Author Closing Comment

by:Boston617
ID: 40514042
that worked perfectly, thank you
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question