Solved

Get rid of Run-Time Error '91'

Posted on 2014-12-22
3
210 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
Comment Utility
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
Comment Utility
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
Comment Utility
that worked perfectly, thank you
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now