?
Solved

Get rid of Run-Time Error '91'

Posted on 2014-12-22
3
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

765 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