Clear specified range of cells if a specific cell value is changed

cjms85

New Member
Joined
Jul 7, 2009
Messages
19
Hi all,

I have a piece of code below which clears of a given row in column J if a cell in the same row in columns G, H or I is changed.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
        If Target.Validation.Type = 3 Then
        Application.EnableEvents = False
    
            Select Case Target.Column
                Case 7
                Target.Offset(0, 3).ClearContents
                Case 8
                Target.Offset(0, 2).ClearContents
                Case 9
                Target.Offset(0, 1).ClearContents
            End Select
        End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub

This seems to work pretty well for my purposes, although I realise this isn't necessarily perfect.

The problem I have, is that I also need to clear some additional cells for a given row when the value in column J is changed.

I've tried using:

Code:
Case 10
Range(Target.Offset(0,3),Target.Offset(0,5),Target.Offset(0,6),Target.Offset(0,7),Target.Offset(0,8)
,Target.Offset(0,9),Target.Offset(0,10).ClearContents

beneath the entry for Case 9, but this doesn't seem to work, as I get the argument "Wrong number of arguments or invalid property".

Can anyone advise what I can do resolve this issue?

The cells that are being cleared, are for the most part all cells with data validation lists, with indirect references to various named ranges. Might this be causing a conflict?

I can share the document if need be, but it's rather large at 31 MB.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You are missing an ending parentheses before .ClearContents.
Also, you can combine the first three cases into one, i.e.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
        If Target.Validation.Type = 3 Then
        Application.EnableEvents = False
              Select Case Target.Column
                Case 7, 8, 9
                    Cells(Target.Row, "J").ClearContents
                Case 10
                    Range(Target.Offset(0, 3), Target.Offset(0, 5), Target.Offset(0, 6), Target.Offset(0, 7), Target.Offset(0, 8) _
                        , Target.Offset(0, 9), Target.Offset(0, 10)).ClearContents
            End Select
        End If

exitHandler:
    Application.EnableEvents = True
    Exit Sub

End Sub
 
Upvote 0
Hi Joe4,
I've just used the code with the changed you've suggested, but I'm still getting "Compile Error: Wrong number or arguments or invalid property assignment". Any idea what might be causing it?
 
Upvote 0
Yes, you cannot specify individual cells like that. In that method, you indicate first and last cell, and it clears everything in between (sorry I missed that the first time around, though the other error I pointed out was true also).

Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    On Error Resume Next
        
    If Target.Validation.Type = 3 Then
        Application.EnableEvents = False
    
        Select Case Target.Column
            Case 7, 8, 9
                Cells(Target.Row, "J").ClearContents
            Case 10
                Target.Offset(0, 3).ClearContents
                Range(Target.Offset(0, 5), Target.Offset(0, 10)).ClearContents
        End Select
     End If

exitHandler:
    Application.EnableEvents = True
    Exit Sub

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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