VBA to clear content in a range based on conditional formatting in a cell on the same row

mathewwheeler

New Member
Joined
Jun 15, 2017
Messages
18
I'm not very strong at VBA and the conditional formatting is based on a lookup value being greater than the value in column B. I would like the code to run anytime that the value in column B is updated.

So I'm creating a model and each row will be an employee - column B is the employee's start date (YR1 M1 (first day of the 5 year model) to YR5 M12 (last day of the model). I have each column (corresponding to a YR/M combination from YR1 M1 to YR5 M12) referencing a numeric value YR1 M1 = .0825, YR1 M2 = .165, YR1 M3 = .2475 ...

If the lookup value of column B is greater than the referenced numeric value, the cell is suppose to turn black (and the goal is to clear the data, so it does not flow through the model). With that detail, could you please help me with the structure of the VBA code? I am also open to changing the condition for formatting/clearing content if there you have any suggestions.

Thank you for any help! These forums are always very helpful in learning/improving.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Say you have this


A
B
1
Employee​
StartDate​
2
emp1​
YR1 M3​
3
emp2​
YR2 M1​
4
emp3​
YR2 M11​
5
emp4​
YR1 M7​
6
emp5​
YR2 M2​
7
emp6​
YR3 M7​
8
emp7​
YR2 M2​
9
emp8​
YR1 M8​
10
emp9​
YR3 M2​

<tbody>
</tbody>


and say the Ref Number is 1.155 that corresponds to YR2 M2

Do you want to delete the rows 4, 7 and 10? Or just clear the contents of columns A and B (or only of column B)?

M.
 
Last edited:
Upvote 0
Hi Marcelo,

Thank you for the reply. I would like to clear the content in columns C - Z based on the value in column B (and have the code run whenever the value in column B is changed).
 
Upvote 0
Hi Marcelo,

Thank you for the reply. I would like to clear the content in columns C - Z based on the value in column B (and have the code run whenever the value in column B is changed).

Questions
Where is the Ref Number entered? Which cell?
Suppose the Ref Number changes: what the code should do? Or it never changes?

M.
 
Last edited:
Upvote 0
The reference number does not change. It is in row 5 of the spreadsheet - each column has a different reference number, because the columns represent the YR M combos.
 
Upvote 0
Row 5 of what column? Please, be specific.

hmm...I'm not understanding what you're trying to do (the purpose of the code).

You said that the code should run every time the value in column B changes. But if you are entering the start date in column B I imagine it is for a new employee and therefore does not yet have data in columns C to Z.

Or can the start date of each employee (which already exists in the database) be changed?

I'm really confused ...:confused:

Please try to explain exactly:
What data already exists;
New rows can be added? How? The Ref Numbers for those rows already exist?
In what row / column is the Ref Number
The purpose of the code.



M.
 
Last edited:
Upvote 0
Sure - sorry for any confusion. Row 5 has the criteria for each column. The conditional format of column C is dependent on the reference number in cell C5, but the formatting of column F depends on the reference number in cell F5, etc.

The model is a forecast, so the start date can change (and most likely WILL change), so that the I can see the different scenarios, and I don't want the old #'s to pull into my model if I change the start date.
 
Upvote 0
So there are Ref Numbers in C5, D5....,Z5 and, for example, the data in column C should be cleared if column B is greater than C5. The same for column D if B > D5 and so on.
Am i right?

A small data sample, ~10 rows 5 columns (A:E), along with the expected outcome would be helpful.

M.
 
Upvote 0
An issue
Suppose in B10 is typed a date greater than C5, then cell C10 is cleared.
If, in the future, B10 is changed to a date less than C5, it will not be possible to retrieve the contents of C10.
Unless there is a static database on another sheet (untouchable) . Maybe possible, but rather complicates the code.

M.
 
Last edited:
Upvote 0
Sure thing example data below

ABCDEFGH
4
Start Date:YR1 M1YR1 M2YR1 M3YR1 M4YR1 M5YR1 M6
5.08.17.25.33.42.5
6EMPLOYEE1
YR1 M1500500500500500500
7EMPLOYEE2YR2 M1
8EMPLOYEE3YR1 M3500500500500

<tbody>
</tbody>

So the return values for the employee start dates would be as below:

EMPLOYEE1: .08
EMPLOYEE2: 1.08
EMPLOYEE3: .25

If I were to change EMPLOYEE1's start date to YR1 M2, I would like for the content in C6 to be cleared based on the VBA criteria (referencing column B). The rule should be, clear the content of any cell where the lookup value is greater than the reference value.
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,679
Members
449,463
Latest member
Jojomen56

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