Conditional code to add Background color to cells

mikemcbain

Board Regular
Joined
Nov 14, 2005
Messages
152
Office Version
  1. 365
Platform
  1. Windows
Happy New Year Magicians,

I would like some code for a macro that I can Run from a button and which will check Column V and add background colour .Color = 65535 to each Cell that contains a Value greater than 999.

And seperate code for another spreadsheet which will check Column BM and if any Cells contain values less than 5.600 then it will add background colour .Color = 65535 to each Cell on those same Rows but in Column K.

Hoping everyone has a very prosperous 2023

Old Mike.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about conditional formatting ?
WB1
KVBM
110006
29995.4
3500
420005.5
56
SH1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K:KExpression=AND(BM1<5.6,BM1<>"")textNO
V:VExpression=V1>999textNO
 
Upvote 0
Solution
How about conditional formatting ?
WB1
KVBM
110006
29995.4
3500
420005.5
56
SH1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
K:KExpression=AND(BM1<5.6,BM1<>"")textNO
V:VExpression=V1>999textNO
Aaahh HongRu that will do very nicely thank you. Mike.
 
Upvote 0
I would like some code for a macro that I can Run
You have a perfectly good solution from @HongRu, but just in case you were interested in using code to add the formatting, try the following:
VBA Code:
Option Explicit
Sub Add_Color_Formatting()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")  '<~~ change to actual sheet name
    Set ws2 = Worksheets("Sheet2")  '<~~ change to actual sheet name
    
    Dim Rng As Range
    Set Rng = ws1.Range("V2", ws1.Cells(Rows.Count, "V").End(xlUp))
    With Rng
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="999"
        .FormatConditions(1).Interior.Color = vbYellow
    End With
    
    Set Rng = ws2.Range("K2:K" & ws2.Cells(Rows.Count, "BM").End(xlUp).Row)
    With Rng
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, _
        Formula1:="=AND(BM2<5.6,BM2<>"""")"
        .FormatConditions(1).Interior.Color = vbYellow
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,199
Members
453,022
Latest member
RobertV1609

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