Worksheet_change event help

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
I have the below code

Code:
Private Sub worksheet_change(ByVal target As Range)

Dim rr As Integer
rr = target.Row

Application.EnableEvents = False

On Error Resume Next

If Not Intersect(target, Columns(1)) Is Nothing Then

    If target.Value = "" Then
        
        Cells(rr, 2).Value = ""

    Else

        Cells(rr, 2).FormulaR1C1 = _
        "=RC1+RC2"

    End If

Application.EnableEvents = True

End If

End Sub

What it does, as you know, is that when column A is deleted, it proceeds in deleting column B of the same row.

The problem is when the user deletes more than 1 row e.g. A2:A10. When the user does this, the code only deletes B2 but not B3:B10.

Is there a way to have the code delete the column B for all affected rows?

Thanks
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
When more than 1 cell is changed at a time, then Target becomes a multiple cell range
A2:A10 in your example.

You can't use a line like
If target.Value = "" Then
When target is a multiple cell range.

You have to loop through each cell in the range..

hang on for example, accidentally hit submit before I was done...
 
Upvote 0
Try

Code:
Private Sub worksheet_change(ByVal target As Range)
Dim MyRange As Range, c As Range
Set MyRange = Intersect(target, Columns(1))
If MyRange Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In MyRange
    If c.Value = "" Then
        Cells(c.Row, 2).Value = ""
    Else
        Cells(c.Row, 2).FormulaR1C1 = _
        "=RC1+RC2"
    End If
Next c
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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