On cell change - Clear Cells in that row

Jayce72

New Member
Joined
Jul 24, 2016
Messages
7
Hi,

I have found code that works that will delete certain cells if I change a key cell. e.g: If I change call B15 then cells C15: F15 get wiped - and the following works ok

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B15)) Is Nothing Then
Range("C15:F15").ClearContents
End If
End Sub


So how can I amend the code so it works on each line from 16 to say 100 - where it'll on wipe the data on the corresponding line?

Thanks :)
 

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.
Hi, you could try like this.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Intersect(Target, Range("B16:B100"))
If Not r Is Nothing Then
    Application.EnableEvents = False
    For Each c In r.Cells
        If c.Value = "" Then c.Offset(, 1).Resize(, 4).ClearContents
    Next c
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Maybe something like

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Row > 15 And Target.Row < 101 And Target.Cells.Count = 1 Then
        Application.EnableEvents = False

        If Not Intersect(Target, Columns(2)) Is Nothing Then
            Range(Cells(Target.Row, 3), Cells(Target.Row, 6)).ClearContents
        End If

        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
Solution
Maybe something like

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Row > 15 And Target.Row < 101 And Target.Cells.Count = 1 Then
        Application.EnableEvents = False

        If Not Intersect(Target, Columns(2)) Is Nothing Then
            Range(Cells(Target.Row, 3), Cells(Target.Row, 6)).ClearContents
        End If

        Application.EnableEvents = True
    End If
End Sub
Thanks - perfect :)(y)
 
Upvote 0
Hi, I don't know if you tried my suggestion or not, but one thing it will do differently is, if you change multiple cells in the range at once, it will still work - not sure if that might be important (or desirable) for you.
 
Upvote 0
Hi, I don't know if you tried my suggestion or not
Hi @FormR the code only clears the cells on the line if you change the Target cell/s to a blank cell, whereas the OP request was just for a change in the cell ;)
 
Upvote 0
the OP request was just for a change in the cell

Thanks Mark, I missed that. Here is my code amended for that.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Intersect(Target, Range("B16:B100"))
If Not r Is Nothing Then
    Application.EnableEvents = False
    For Each c In r.Cells
       c.Offset(, 1).Resize(, 4).ClearContents
    Next c
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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