Color cell based on value gathered

boninm

New Member
Joined
Jan 31, 2025
Messages
13
Office Version
  1. 365
Platform
  1. Windows
HI

I'm using a VBA code to check if current date cell should be flagged based on a date to check against.
The code works fine, except cells don't want to change to color required.
Check output variable with msgbox


CHECK AGAINST DATE.xlsm
ABCDEFGHIJK
1Today's date2025-02-26<<<= change cell color =>>>2025-02-26
2
3Date to check against:2026-01-012025-03-31
4
5if less than 365 but greather than 90 - Cell = Yellow
6
7If less or equal to 90 - cell = Red
8
9
10Using VBA code to check if current date should be flagged based on a date to check against
11Code works fine except cells don't want to change to color required.
12
13
14
Sheet1
Cell Formulas
RangeFormula
F1,K1F1=NOW()



The VBA code under ThisWorkbook

'Private Sub Workbook_Open()
Sub CheckDate()

Dim VToday1
Dim VCellValue1
Dim VToday2
Dim VCellValue2
Dim VTimeLeft1 As Integer
Dim VTimeLeft2 As Integer

' VToday1 , VCellValue1 , VToday2 , VCellValue2 , VTimeLeft1 , VTimeLeft2

VToday1 = DateValue(Left(Cells(1, "F").Value, 10))
'MsgBox VToday1
VCellValue1 = Cells(3, "F").Value
'MsgBox VCellValue1
VTimeLeft1 = VCellValue1 - VToday1
'MsgBox VToday1, VCellValue1, VTimeLeft1
Cells(2, "F").Value = VTimeLeft1
'MsgBox VTimeLeft1
If VTimeLeft1 > 365 And VTimeLeft1 < 90 Then
Range("F1").Interior.ColorIndex = 6 ' yellow
End If

If VTimeLeft1 <= 90 Then
Range("F1").Interior.ColorIndex = 3 ' red
End If
'MsgBox VTimeLeft1




VToday2 = DateValue(Left(Cells(1, "K").Value, 10))
'MsgBox VToday2
VCellValue2 = Cells(3, "K").Value
'MsgBox VCellValue2
VTimeLeft2 = VCellValue2 - VToday2
'MsgBox VToday2, VCellValue2, VTimeLeft2
Cells(2, "K").Value = VTimeLeft2
'MsgBox VTimeLeft2
If VTimeLeft2 > 365 And VTimeLeft2 < 90 Then
Range("K1").Interior.ColorIndex = 6 ' yellow
End If

If VTimeLeft2 <= 90 Then
Range("K1").Interior.ColorIndex = 3 ' red
End If
'MsgBox VTimeLeft2

Application.Wait (Now + TimeValue("00:00:05"))
End Sub

------------------------------

And thank you in advance for helping.

Maurice
 
Why are you using VBA to do this, when it appears that it can probably be done pretty easily using Conditional Formatting?
No sense in re-creating built-in functionality that works dynamically with no code, if you do not have to.
 
Upvote 0
Solution
Why are you using VBA to do this, when it appears that it can probably be done pretty easily using Conditional Formatting?
No sense in re-creating built-in functionality that works dynamically with no code, if you do not have to.
I want to run this when i open the spreadsheet

if conditional formatting, how would you do this?
I've used conditional formatting before, but not to the extent that i want to do here.

Thanks for helping.
 
Upvote 0
Conditional Formatting is real-time, and runs all the time.
So as soon as you make any data changes, it automatically updates, as well as when the file is opened.
 
Upvote 0
OK, just note that you will probably need to use the formula option:

1740574502286.png


If you are trying to apply it to a multi-cell range, just select the range you want to apply it to before writing this formula, and then write the formula as it applies to the very FIRST cell in your selected range. Excel will adjust the formula it for all the other cells in the selected range. Just be sure to use "$" to anchor down any range that you don't want to change as the formula changes for other cells.

For example, let's say that you simply wanted to see if all the cells in B1:B10 are equal to the value in cell A1.
Then you would select the range B1:B10 and use this Conditional Formatting formula:
Excel Formula:
=B1=$A$1

Since you have multiple colors, you will need multiple Conditional Formatting rules (one for each color).
Post back here if you run into any trouble.
 
Upvote 0

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