Change Event for Horizontal Range

Victor Moss

Board Regular
Joined
May 3, 2014
Messages
90
Hi Guys,

I have 2 separate ranges, A5:G6000 & N5:Q6000, when I clear contents of all cells in a row in one range, not only some, I'd like the cells in the same row in the other range to be cleared. I know it's a Worksheet_Change event as I have a couple in my Sheet already, but I don't know how to code this one.

Any help would be appreciated.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Cr1 As Long, Cr2 As Long
If Not Intersect(Target, Range("A5:G6000")) Is Nothing Then
Cr1 = Application.WorksheetFunction.Count(Target)
If Cr1 = 0 Then Range("N5:Q6000").ClearContents
End If
If Not Intersect(Target, Range("N5:Q6000")) Is Nothing Then
Cr2 = Application.WorksheetFunction.Count(Target)
If Cr2 = 0 Then Range("A5:G6000").ClearContents
End If
End Sub
 
Upvote 0
Thank you Maabadi,

If Cr1 = 0 Then Range("N5:Q6000").ClearContents

I don't want to clear the entire range, I just want to clear cells in the target row of Range("N5:Q6000")

So If Range("A6:G6") are cleared, then Range("N6:Q6") must be cleared, if that explains it a bit more clearly. :)
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Cr1 As Long
If Not Intersect(Target, Range("A5:G6000")) Is Nothing Then
Application.EnableEvents = False
Debug.Print Target.Rows.Count
Cr1 = Application.WorksheetFunction.Count(Target)
If Cr1 = 0 Then Range("N" & Target.Row & ":Q" & Target.Row + Target.Rows.Count - 1).ClearContents
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long, Cr1 As Long
If Not Intersect(Target, Range("A5:G6000")) Is Nothing Then
Application.EnableEvents = False
Debug.Print Target.Rows.Count
Cr1 = Application.WorksheetFunction.Count(Target)
If Cr1 = 0 Then Range("N" & Target.Row & ":Q" & Target.Row + Target.Rows.Count - 1).ClearContents
Application.EnableEvents = True
End If
End Sub
Perfect, thank you very much.
 
Upvote 0
You're Welcome & Thanks for Feedback.
 
Upvote 0
You're Welcome & Thanks for Feedback.
Apologies, I know we concluded this, but I was as well looking to do this:

If Not Application.Intersect(Target, Range("A5:Q6000")) Is Nothing Then
If Cells(Target.Row, 1) = "" And Cells(Target.Row, 2) = "" And Cells(Target.Row, 3) = "" And Cells(Target.Row, 4) = "" And Cells(Target.Row, 5) = "" And Cells(Target.Row, 6) = "" Then
Application.EnableEvents = False
Cells(Target.Row, 14).ClearContents
Cells(Target.Row, 15).ClearContents
Cells(Target.Row, 16).ClearContents
Cells(Target.Row, 17).ClearContents
Application.EnableEvents = True
End If
End If

The only problem I have here is that if more than 1 row is cleared, only the first row is affected by the Change.
 
Upvote 0
If you want Use your code change it to this:
VBA Code:
If Not Application.Intersect(Target, Range("A5:Q6000")) Is Nothing Then
If Cells(Target.Row, 1) = "" And Cells(Target.Row, 2) = "" And Cells(Target.Row, 3) = "" And Cells(Target.Row, 4) = "" And Cells(Target.Row, 5) = "" And Cells(Target.Row, 6) = "" Then
Application.EnableEvents = False
Range(Cells(Target.Row, 14),  Cells(Target.Row + Target.Rows.Count - 1, 17).ClearContents
Application.EnableEvents = True
End If
End If
 
Upvote 0
If you want Use your code change it to this:
VBA Code:
If Not Application.Intersect(Target, Range("A5:Q6000")) Is Nothing Then
If Cells(Target.Row, 1) = "" And Cells(Target.Row, 2) = "" And Cells(Target.Row, 3) = "" And Cells(Target.Row, 4) = "" And Cells(Target.Row, 5) = "" And Cells(Target.Row, 6) = "" Then
Application.EnableEvents = False
Range(Cells(Target.Row, 14),  Cells(Target.Row + Target.Rows.Count - 1, 17).ClearContents
Application.EnableEvents = True
End If
End If
Excellent. Thank you very much. I wish I had the same expertise as you.
 
Upvote 0

Forum statistics

Threads
1,215,235
Messages
6,123,789
Members
449,126
Latest member
Greeshma Ravi

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