Object variable or With block variable not set error???

Disgruntled77

New Member
Joined
Oct 29, 2018
Messages
1
Good day all,

I have reached the end of my own limited knowledge and ask if anyone here could help me?

I have a simple spreadsheet, column(5) "E", has a pulldown with values P1 thru P4. The following code simply places an 'X' in column "A" when P1 is selected, and removes the 'X' when any other value is chosen.

My problem is when any other cell is edited, I receive a 'Object variable or With block variable not set error'...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
    If Target.Cells.CountLarge > 1 Then Exit Sub
      Application.EnableEvents = False
    
    If Intersect(Target, Columns(5)) = "P1" Then
      If Target.Row > 1 Then
        Range("A" & Target.Row).Value = "X"
    End If
      End If
    
    If Not Intersect(Target, Columns(5)) = "P1" Then
      If Target.Row > 1 Then
            Range("A" & Target.Row).Value = ""
    End If
      End If
             
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

Any help is greatly appreciated.

Sincerely,
Ryan
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi & welcome to MrExcel
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
    If Target.Cells.CountLarge > 1 Then Exit Sub
      Application.EnableEvents = False
    
    If Not Intersect(Target, Columns(5)) Is Nothing Then
      If Target.Row > 1 Then
        Range("A" & Target.Row).Value = IIf(Target.Value = "P1", "X", "")
      End If
   End If
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
 
Upvote 0
You need to include for the condition where there is no intersect.

You get the error because you try to do a logical check on a range (which is the intersect) where the range is nothing.

That's what it means. I added code to check to see if the intersect is nothing, and if so, exit the codee

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
    If Intersect(Target, Columns(5)) Is Nothing Then Exit Sub
    If Target.Cells.CountLarge > 1 Then Exit Sub
      Application.EnableEvents = False

    
    If Intersect(Target, Columns(5)) = "P1" Then
      If Target.Row > 1 Then
        Range("A" & Target.Row) = "X"
    End If
      End If
    
    If Not Intersect(Target, Columns(5)) = "P1" Then
      If Target.Row > 1 Then
            Range("A" & Target.Row).Value = ""
    End If
      End If
             
Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,127
Members
453,021
Latest member
Justyna P

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