Run Time error '91'

pete4monc

Board Regular
Joined
Jan 12, 2014
Messages
85
Office Version
  1. 365
Platform
  1. Windows
Hi guys
I have this code which automatically runs when a cell in column J is populated from a drop down box, which works fine.
But if I type anything in any of the adjacent cells or delete any text in any adjacent cells it comes up with a run time error and I am not sure if it is my code or just something simple (like me.....lol).
It never used to come up with this error until I put the code in?
Does anyone have an idea what this could be? Many Thanks.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("J:J"), Target)
xOffsetColumn = 11
If WorkRng = "COMPLETE" Then
    Application.EnableEvents = False
    For Each Rng In WorkRng
        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
        Else
            Rng.Offset(0, xOffsetColumn).ClearContents
        End If
    Next
    Application.EnableEvents = True
End If
End Sub

Annotation 2022-07-05 073733.png
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I guess that that error comes from the range named WorkRng is nothing.
If you use an event only for column J, your code can be more simple as follows.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Const xOffsetColumn As Integer = 11

    'Make this event available only user changes Column J
    If Target.Column <> 10 Then Exit Sub
    If Target.Count > 1 Then Exit Sub

    Application.EnableEvents = False
    Select Case Target.Value
    Case "COMPLETE"
        Target.Offset(, xOffsetColumn).Value = Now
    Case ""
        Target.Offset(, xOffsetColumn).ClearContents
    End Select
    Application.EnableEvents = True
End Sub
 
Upvote 0
In case you may clear (or enter) a number of cells in column J at once, I would suggest the following modification so that it acts on all the relevant rows.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Changed As Range, cell As Range
    
    Const xOffsetColumn As Integer = 11

    Set Changed = Intersect(Target, Columns("J"))
    If Not Changed Is Nothing Then
      Application.EnableEvents = False
      For Each cell In Changed
        Select Case cell.Value
          Case "COMPLETE"
              cell.Offset(, xOffsetColumn).Value = Now
          Case ""
              cell.Offset(, xOffsetColumn).ClearContents
        End Select
      Next cell
      Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Solution
I guess that that error comes from the range named WorkRng is nothing.
If you use an event only for column J, your code can be more simple as follows.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Const xOffsetColumn As Integer = 11

    'Make this event available only user changes Column J
    If Target.Column <> 10 Then Exit Sub
    If Target.Count > 1 Then Exit Sub

    Application.EnableEvents = False
    Select Case Target.Value
    Case "COMPLETE"
        Target.Offset(, xOffsetColumn).Value = Now
    Case ""
        Target.Offset(, xOffsetColumn).ClearContents
    End Select
    Application.EnableEvents = True
End Sub
@Colo
Thanks that works great. Sometimes we try to make things too complicated unnecessarily.....lol. Thank you!
 
Upvote 0
In case you may clear (or enter) a number of cells in column J at once, I would suggest the following modification so that it acts on all the relevant rows.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Changed As Range, cell As Range
   
    Const xOffsetColumn As Integer = 11

    Set Changed = Intersect(Target, Columns("J"))
    If Not Changed Is Nothing Then
      Application.EnableEvents = False
      For Each cell In Changed
        Select Case cell.Value
          Case "COMPLETE"
              cell.Offset(, xOffsetColumn).Value = Now
          Case ""
              cell.Offset(, xOffsetColumn).ClearContents
        End Select
      Next cell
      Application.EnableEvents = True
    End If
End Sub
@Peter_SSs
Hi Peter, thanks for that, this works brilliant. Many thanks for yourhelp.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,945
Members
449,198
Latest member
MhammadishaqKhan

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