Solved

Get rid of Run-Time Error '91'

Posted on 2014-12-22
3
216 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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