Conditional code clears cells all the time not just when condition is met.

SewerUrchin

New Member
Joined
Jan 25, 2005
Messages
24
Hi folks:

Sorry this is longish . . . A workbook that allows a distributed team to report weekly status via a spreadsheet in the same. Conditional formatting is used as much as it can be. In one case VB is needed.

Each sheet has a task in each row, dedicated to reporting task status. There are 5 possible values for the status field. They are : Red, Yellow, Green, Done & Unused. If status is yellow or red, then two new columns have to be filled in: Impact to team and Action Plan. Here is where the quandary is found . . .

If the status goes from red or yellow, to any value other than red or yellow, the Impact to team and Action Plan fields should be cleared for that row. (conditional formatting cross hatches it). The problem: the code blanks everything, all the time, not just when the row status value is something other than red or yellow. I can’t figure out why though.

Here is a sample of the code I’ve used (I’m a novice so please if you know of a way to shorten the code so it iterates rather than having to list code for each row, I’m way open to it):

Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Rng1 As Range
On Error Resume Next

If Sheets("PI").Range("F6").Value = "Green" Or "Done" Or "Unused" Then
Set Rng1 = Sheets("PI").Range("G6,H6")
Rng1.ClearContents
Else
End If

If Sheets("PI").Range("F7").Value = "Green" Or "Done" Or "Unused" Then
Set Rng1 = Sheets("PI").Range("G7,H7")
Rng1.ClearContents
Else
End If

This code is repeated for each row (a total of 20 of them). I’ve used Rng1 over because it would seem that once you are past the preceding block, remapping the range wouldn’t be an issue . . . But maybe it is. As I said, I’m a very novice user of VB.

My question is: Why does this code clear the Impact to team and Action Plan cells all the time, instead of only doing it when the value in column F is something other than red/yellow?

Your help is very much appreciated!

SU
 

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.
The correct syntax is

Code:
If Sheets("PI").Range("F6").Value = "Green" Or Sheets("PI").Range("F6").Value = "Done" Or Sheets("PI").Range("F6").Value = "Unused" Then
 
Upvote 0
It might be easier or more visually apealling to use Select Case.
Particularly if there are more than just a few OR criteria..

<font face=Error: Key or Value Not Found.><SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> Sheets("PI").Range("F6").Value<br>    <SPAN style="color:#00007F">Case</SPAN> "Green", "Done", "Unused"<br>        <SPAN style="color:#00007F">Set</SPAN> Rng1 = Sheets("PI").Range("G6,H6")<br>        Rng1.ClearContents<br>    <SPAN style="color:#00007F">Case</SPAN> <SPAN style="color:#00007F">Else</SPAN><br>        <SPAN style="color:#007F00">'Do Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN></FONT>
 
Upvote 0
Beautiful John!

I'll have to adjust the code and utilize your, more elegant syntax. Thanks!

Jeff - AKA Sewer Urchin
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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