0

This likely is easier than I am making it out to be, but I am struggling mightily with a pretty simple concept.

I have 3 columns in my excel sheet that I need to validate with one another whenever a value in any of them changes. The columns are Q, R, and S.

  • If I enter/change a value in Q, I want it to clear out any values that might be in R or S of the same row.
  • If I enter/change a value in R, I want it to clear out any values that might be in Q or S of the same row.
  • If I enter/change a value in S, I want it to clear out any values that might be in Q or R of the same row.

I can get it to work fine for just one of those conditions, but when I start adding ELSE statements to the VBA, I get an RTE ("Method 'ClearContents' of object 'Range' failed").

This code works fine. If I have a value in either R or S (or both)...when I change Q it clears out R and S:

Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 17) Then
    If Not Intersect(Target, Range("Q" & Target.Row)) Is Nothing Then
        Target.Offset(0, 1).ClearContents
        Target.Offset(0, 2).ClearContents
    End If
End If
End Sub

But, when I try to do the following, I get an RTE when I try it and Excel essentially freezes up on me and I have to kill it in Task Manager:

Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Column = 17) Then
    If Not Intersect(Target, Range("Q" & Target.Row)) Is Nothing Then
        Target.Offset(0, 1).ClearContents
        Target.Offset(0, 2).ClearContents
    End If
Else
    If (Target.Column = 18) Then
        If Not Intersect(Target, Range("R" & Target.Row)) Is Nothing Then
            Target.Offset(0, -1).ClearContents
            Target.Offset(0, 1).ClearContents
        End If
    Else
        If (Target.Column = 19) Then
            If Not Intersect(Target, Range("S" & Target.Row)) Is Nothing Then
                Target.Offset(0, -1).ClearContents
                Target.Offset(0, -2).ClearContents
            End If
        End If
    End If
End If
End Sub

I'm not by any means a VBA or Excel pro but I just don't see why the above wouldn't work. The debugger in here seems pretty rudimentary and I can't get any specific error to show up (is there something I should be able to see??)

I tried doing Range(col & Target.Row).ClearContents instead of using the Target.Offset but it yields the same issue. Really appreciate any help that can be given with this. Thanks!

svenGUTT
  • 399
  • 4
  • 11
  • See the linked duplicate... clearing contents causes the event to re-fire in an infinite loop. – BigBen Oct 02 '20 at 19:00
  • Much obliged!! That post solved my problem. Infinite loop makes perfect sense and I feel pretty stupid for not realizing that was happening. Thanks! – svenGUTT Oct 02 '20 at 19:21

0 Answers0