VBA Color cell range based on other cell value

JAZ91

New Member
Joined
Sep 28, 2022
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi

For certain reasons I want to make this in VBA and not conditional formatting in excel.

I have a table of data where I want to color certain cells (a range) based on a value.
Fx
I want to color cell E4, F4, G4, H4, I4 and J4 based on the value in K4.
The value in K4 can be would be a due date so coloring could be something like gradual from green to red depending on how many days are left, or perhaps 1days left, 3days left, 7days,14 days left and +14days left.

And in the same row I also want to color in possibly a different color the
cell N4, O4 and P4 based on the value in P4.
The value in P4 can be open, closed, done, on hold.

I have searched but not found something where I can specify a specific range to color but only singe cells.
And since im new to VBA I have not managed to create something myself.

Hope someone can help :)
 

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.
I have searched but not found something where I can specify a specific range to color but only singe cells.
If that is the crux of your problem then post code to color a single cell and we'll show you how to modify it for any range.

The "gradual from green to red" is the hardest part of this. Yes, certainly possible, but it would take me a while to write the code for your whole description and I'm sorry that don't have the time to do it all.
 
Upvote 0
If that is the crux of your problem then post code to color a single cell and we'll show you how to modify it for any range.

The "gradual from green to red" is the hardest part of this. Yes, certainly possible, but it would take me a while to write the code for your whole description and I'm sorry that don't have the time to do it all.
What I got so far is this code. However it was working for one column and coloring the cells but it stopped working. And I also cannot seem to assign a macro button for some reason since it don't show up in the macro list. I originally wanted this to be automatic but since it wasn't working I tried to make a button to see if that helped, which was when I discovered I was not able to do even that. (ps. It may be something don't make sense in the code since I have copied something i searched up and tried to adapt it to my sheet.

Kind regards

JAZ

Sub Check_status(ByVal Target As Range)

Application.ScreenUpdating = False

Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition, cond4 As FormatCondition
Set rg = Range("A4:P200", Range("A4:P200").End(xlDown))

'clear any existing conditional formatting
rg.FormatConditions.Delete

'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "Closed")
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "Open")
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, "On hold")
Set cond4 = rg.FormatConditions.Add(xlCellValue, xlEqual, "Done")

'define the format applied for each conditional format
With cond1
.Interior.Color = vbGreen
.Font.Color = vbBlack
End With

With cond2
.Interior.Color = vbRed
.Font.Color = vbWhite
End With

With cond3
.Interior.Color = vbYellow
.Font.Color = vbRed
End With

With cond4
.Interior.Color = vbBlue
.Font.Color = vbWhite
End With



Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,109
Members
452,302
Latest member
TaMere

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