VBA Error code 91

Notrom

New Member
Joined
Nov 8, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I am a novice VBA user and I just can't get this code to work without error codes. Can some one see why it's not working?

When a drop down list in cells in column B show either of the 2 target phrases, the MsgBox should appear, which it does. However, when I click into other cells, the error code appears!

VBA Code.png
 

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.
Welcome to the Board!

That is because Intersect returns a range. If the update is outside of column I, it will return the empty range. You cannot check the value of an empty range.

Try something like this instead:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
    Set rng = Intersect(Target, Range("I5:I1500"))
    
'   Exit if cell updated not in watched range
    If rng Is Nothing Then Exit Sub
    
'   Loop through updated cells in watched range
    For Each cell In rng
        If cell.Value = "Change in Working Pattern" Or cell.Value = "New Staff Member" Then
            MsgBox "Please input the hours..."
        End If
    Next cell
    
End Sub

Also, in the future, please post your actual VBA code instead of just an image of it, so we can copy it over and edit it (you cannot do that with an image!).
See here for details on how to do that: How to Post Your VBA Code
 
Upvote 0
Welcome to the Board!

That is because Intersect returns a range. If the update is outside of column I, it will return the empty range. You cannot check the value of an empty range.

Try something like this instead:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
  
    Set rng = Intersect(Target, Range("I5:I1500"))
  
'   Exit if cell updated not in watched range
    If rng Is Nothing Then Exit Sub
  
'   Loop through updated cells in watched range
    For Each cell In rng
        If cell.Value = "Change in Working Pattern" Or cell.Value = "New Staff Member" Then
            MsgBox "Please input the hours..."
        End If
    Next cell
  
End Sub

Also, in the future, please post your actual VBA code instead of just an image of it, so we can copy it over and edit it (you cannot do that with an image!).
See here for details on how to do that: How to Post Your VBA Code

Welcome to the Board!

That is because Intersect returns a range. If the update is outside of column I, it will return the empty range. You cannot check the value of an empty range.

Try something like this instead:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
   
    Set rng = Intersect(Target, Range("I5:I1500"))
   
'   Exit if cell updated not in watched range
    If rng Is Nothing Then Exit Sub
   
'   Loop through updated cells in watched range
    For Each cell In rng
        If cell.Value = "Change in Working Pattern" Or cell.Value = "New Staff Member" Then
            MsgBox "Please input the hours..."
        End If
    Next cell
   
End Sub

Also, in the future, please post your actual VBA code instead of just an image of it, so we can copy it over and edit it (you cannot do that with an image!).
See here for details on how to do that: How to Post Your VBA Code
Thank you so much for the speedy reply.

Sorry I posted incorrectly, I was rushing! I will check all the FAQ and other info before I post again.

I will try this code as soon as I can, I do appreciate your reply :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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