VBA: Conditional Formatting one column based on another

jazzhayward

New Member
Joined
Sep 16, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have numerical values in column K and a currency text in column O (NZD, USD, AUD etc). I've included a dummy table below.

I want to have the numerical value in column K (Fee) formatted to a red font colour if the text in column O (Invoice Currency) on the same row is not NZD. The data has a varying number of rows.

I've tried the below code (see Column K format), but at the moment it's formatting all the values in column K to a red font colour. I'm assuming it has something to do with the formula in the conditional formatting statement. I thought using $O3 would mean it cycles down but I'm assuming I'll need to set a similar range.

I do have some code written for formatting Column O for a different set of parameters (see Column O format). This ideally will sit before the formatting for column K. I'm not sure if any of the code is useful for the column K formatting but thought I would paste it regardless. Both of these sections will be part of a larger code.

Can you please let me know where I'm going wrong?

Thanks in advance :)

Column K Format
VBA Code:
Sub TestingTextFormatting2()

Dim LRKcol As Long

    LRKcol = Cells(Rows.Count, "K").End(xlUp).Row

Dim rngrng As Range
Dim rngCell As Range
Set rngrng = Range("K3:K" & LRKcol)

For Each rngCell In rngrng
With rngCell
.FormatConditions.Add Type:=xlExpression, Formula1:=" =$O3<>""NZD"" "
With .Font
.Color = vbRed
End With
End With

Next rngCell

End Sub

Column O Format
VBA Code:
Sub TestingTextFormatting()

Dim LROcol As Long
    
    LROcol = Cells(Rows.Count, "O").End(xlUp).Row

With Range("O3:O" & LROcol).FormatConditions.Add(xlTextString, TextOperator:=xlDoesNotContain, String:="NZD")
With .Font
.Color = vbRed
End With
End With

End Sub

CustomerProgram NameRevenue OfficeSales OfficeDelivery OfficeDelivery Office RegionStart DateClient NameClient NameUnbilled FeesFeePrimary AELast Billing Status DateLast Updated ByInvoice Currency
ABCName1The MoonNew YorkAucklandNew Zealand
13/05/2024​
Destinee BrooksDestinee Brooks
2250​
2250​
Asher Ross
13/05/2024​
Mouse, MickeyNZD
DEFName2The MoonNew YorkAucklandNew Zealand
22/05/2024​
Malakai MataMalakai Mata
1450​
1450​
Karly Caldwell
24/05/2024​
Mouse, MickeyNZD
GHIName3The MoonNew YorkAucklandNew Zealand
13/03/2024​
Alonso DouglasAlonso Douglas
1950​
1950​
Marie Kent
27/05/2024​
Mouse, MickeyNZD
ABC1Name4The MoonNew YorkAucklandNew Zealand
20/05/2024​
Bennett JacobsBennett Jacobs
7000​
7000​
Gaven Salazar
24/05/2024​
Mouse, MickeyNZD
DEF1Name5The MoonNew YorkAucklandNew Zealand
18/04/2024​
Javier PhelpsJavier Phelps
2500​
2500​
Erin Wiggins
01/05/2024​
Mouse, MickeyNZD
GHI1Name6The MoonNew YorkAucklandNew Zealand
20/05/2024​
Annalise HendersonAnnalise Henderson
2552​
2552​
Cale Chase
24/05/2024​
Mouse, MickeyNZD
ABC11Name7The MoonNew YorkAucklandNew Zealand
20/05/2024​
Meadow PollardMeadow Pollard
989.38​
989.38​
Konner Joseph
24/05/2024​
Mouse, MickeyNZD
DEF11Name8The MoonNew YorkAucklandNew Zealand
22/05/2024​
Melvin CabreraMelvin Cabrera
3045​
3045​
Trenton Glover
24/05/2024​
Mouse, MickeyNZD
GHI11Name9The MoonNew YorkAucklandNew Zealand
02/05/2024​
Jaslene SaundersJaslene Saunders
800​
800​
Reid Benitez
15/05/2024​
Mouse, MickeyUSD
ABC111Name10The MoonNew YorkAucklandNew Zealand
07/05/2024​
Simon GibbsSimon Gibbs
800​
800​
Jazmin English
15/05/2024​
Mouse, MickeyUSD
DEF111Name11The MoonNew YorkAucklandNew Zealand
20/05/2024​
Karley MejiaKarley Mejia
800​
800​
Zachariah Hall
24/05/2024​
Mouse, MickeyUSD
GHI111Name12The MoonNew YorkAucklandNew Zealand
07/05/2024​
Tamara TaylorTamara Taylor
800​
800​
Cadence Rogers
15/05/2024​
Mouse, MickeyUSD
ABC1111Name13The MoonNew YorkAucklandNew Zealand
02/05/2024​
Ronan DownsRonan Downs
800​
800​
Kaylah Pham
15/05/2024​
Mouse, MickeyUSD
DEF1111Name14The MoonNew YorkAucklandNew Zealand
03/05/2024​
Jairo CardenasJairo Cardenas
800​
800​
Linda Young
15/05/2024​
Mouse, MickeyUSD
GHI1111Name15The MoonNew YorkAucklandNew Zealand
06/05/2024​
Jaylin PattersonJaylin Patterson
800​
800​
Camryn Ali
15/05/2024​
Mouse, MickeyUSD
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Because it's difficult to get the quotes right I always find it easier to build the format expression in a string and then use the string as the formula. You can do the whole range at once - you don't need to go cell by cell.

VBA Code:
Sub TestingTextFormatting2()
    Dim LRKcol As Long
    Dim formatCon As String
    Dim rngrng As Range
    LRKcol = Cells(Rows.Count, "K").End(xlUp).Row
    Set rngrng = Range("K3:K" & LRKcol)
    formatCon = "=$O3<>" & """NZD"""
    With rngrng
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, Formula1:=formatCon
        With .FormatConditions(1).Interior
            .Color = vbRed
        End With
    End With
End Sub
 
Upvote 0
Thanks for your help! I had seen solutions where the formula was built as a string but I couldn't get it to work haha.
 
Upvote 0

Forum statistics

Threads
1,224,899
Messages
6,181,627
Members
453,058
Latest member
rmd0725

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