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 the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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