VBA: run time error 91

jacquesB

New Member
Joined
Jan 4, 2016
Messages
44
Hi guys

I am trying to run the code below but I get a run time error 91 stating: "Object variable or With block variable not set".

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Intersect(Target, Range("O25:O50")).Value = 2 Then
        MsgBox "This is a warning message", vbOKOnly & vbCritical, "Wrong Data Entry"
    End If
    If Not Intersect(Target, Range("G8")) Is Nothing Then
        Q = MsgBox("Do you want end session?", vbQuestion + vbYesNo, "OPTION")
        If Q = vbYes Then
            Sh.Protect "Carlsberg" 'Substitute actual password here
        End If
    End If

End Sub

When I use the debugger it says that the error lies in the second line of code. I.e.
Code:
If Intersect(Target, Range("O25:O50")).Value = 2 Then

I hope that someone on this forum can see a way to fix it as it is way beyond my VBA knowledge.

Thank you!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
That is not valid syntax.
What exactly are you trying to do with this line?
Code:
If Intersect(Target, Range("O25:O50")).Value = 2 Then
Since the intersection of two ranges returns another range, which could be multiple cells, it doesn't really make sense to ask if that range is equal to 2 (since there could be more than one cell in that range).

Are you trying to count the number of cells in that intersected range? If so, use .Count instead of .Value.
If you want to see if any cells in that intersected range equal 2, I would loop through each cell in the range and check them individually.
 
Upvote 0
I am trying to do the latter and see if any cell contain the value 2.

How do you make the loop? I am sorry for my missing expertise :(
 
Upvote 0
That block might look something like this:
Code:
    Dim myRange As Range
    Dim cell As Range
    
    Set myRange = Intersect(Target, Range("O25:O50"))
    If myRange.Count > 0 Then
        For Each cell In myRange
            If cell.Value = 2 Then MsgBox "This is a warning message", vbOKOnly & vbCritical, "Wrong Data Entry"
        Next cell
    End If
 
Upvote 0

Forum statistics

Threads
1,214,846
Messages
6,121,905
Members
449,054
Latest member
luca142

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