Change event removes contents outside of target range

rex759

Well-known Member
Joined
Nov 8, 2004
Messages
608
Office Version
  1. 365
Platform
  1. Windows
Hello,
Here’s snippet of a Private Sub Worksheet_Change(ByVal Target as range)

Code:
If Not Intersect(Target, Range("B4:B27")) Is Nothing And Cells(Target.Row, "B") <> "Surveys" Or Cells(Target.Row, "B") <> "Reviews" And Cells(Target.Row, "D").Value > 0 Then
        Cells(Target.Row, "D").Value = ""
End If

The problem I’m having is its removing header information in range D1, D2 & D3. Is there a way to have the code change rows 4:27 only?

Any help is appreciated
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I think the issue is with your mix of AND and ORs, and not using any parentheses to clarify what needs to be checked and when.
Just guessing, but tty and see if this is what you are after (note I broke it up to three lines to show the three different levels we are checking):
Code:
If (Not Intersect(Target, Range("B4:B27")) Is Nothing) _
    And (Cells(Target.Row, "B") <> "Surveys" Or Cells(Target.Row, "B") <> "Reviews") _
    And (Cells(Target.Row, "D").Value > 0) Then
        Cells(Target.Row, "D").Value = ""
End If
 
Upvote 0
Thank you! Still digesting the parentheses placement but it works perfectly.
 
Upvote 0
You are welcome!

Think of the parentheses as "sets of conditions". So it is really:
Set1 AND Set2 AND Set3
Note that the OR statement is within Set2. Without the parentheses, the mixture of AND or OR won't work the way you want.
 
Upvote 0

Forum statistics

Threads
1,215,216
Messages
6,123,669
Members
449,114
Latest member
aides

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