Clearing cells if a dropdown selection changes

Robzx

New Member
Joined
Jun 5, 2015
Messages
25
Hi all

I'm trying to write some code that will automatically clear the contents of two cells, E3 and F3 when the dropdown selection in column C3 changes, and for a range of rows 3 to 17.

The code that I have is

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range

    For Each Cell In Target
        If Cell.Address = "$C$3:$C$17" Then
            Application.EnableEvents = False
                Range("$E$3:$F$17").ClearContents
            Application.EnableEvents = True
        End If
    Next Cell

End Sub

But I can't quite get it to work, what have I missed?

Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
maybe this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("$C$3:$C$17")) Is Nothing Then Exit Sub
Application.EnableEvents = False
    Range("$E$3:$F$17").ClearContents
Application.EnableEvents = True
End Sub
 
Upvote 0
Thank you Michael, but when one cell selection is changed, the entire range is cleared. I'm trying to only clear E3 and F3, when C3 changes, but any cell in the range of C3:C17 could be changed.
 
Upvote 0
Sorry, I must have misunderstood your request...try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("$C$3:$C$17")) Is Nothing Then Exit Sub
Application.EnableEvents = False
    Target.Offset(0, 2).ClearContents
    Target.Offset(0, 3).ClearContents
Application.EnableEvents = True
End Sub
 
Upvote 0
Sorry, I must have misunderstood your request...try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("$C$3:$C$17")) Is Nothing Then Exit Sub
Application.EnableEvents = False
    Target.Offset(0, 2).ClearContents
    Target.Offset(0, 3).ClearContents
Application.EnableEvents = True
End Sub
Michael, I think that needs some more careful consideration.
With data in all columns up to column J, try selecting B3:C4 or C6:F10 and pressing the Delete key.

My suggestion is:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range

  Set Changed = Intersect(Target, Range("C3:C17"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    Changed.Offset(0, 2).ClearContents
    Changed.Offset(0, 3).ClearContents
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
Gents, thank you very much for the help. It looks as though Peter's suggestion is the most efficient for my needs. If it is a larger range of cells that needs clearing would it be

Code:
    Application.EnableEvents = False
    Changed.Offset(0, 2).ClearContents
    Changed.Offset(0, 3).ClearContents
    Changed.Offset(0, 4).ClearContents
    Changed.Offset(0, 5).ClearContents
    Changed.Offset(0, 6).ClearContents
    Changed.Offset(0, 7).ClearContents

and so on until the nth column is reached or could the upper limit be specified?

Thank you once again for your help
 
Upvote 0
Depends on just what cells you want to clear, but here is one way to do a larger set of columns.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range

  Set Changed = Intersect(Target, Range("C3:C17"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    Intersect(Changed.EntireRow, Columns("E:K")).ClearContents
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
you could use a loop to do it
Code:
for x = 2 to 7
Changed.Offset(0, x).ClearContents
next x

BUT Peters code is far more efficient !!!
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,052
Latest member
Fuddy_Duddy

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