VBA Conditional Formatting

bino1121

New Member
Joined
Apr 26, 2023
Messages
14
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
Platform
  1. Windows
Currently confused about VBA conditional formatting I have not been able to find an example too similar to mine that I can edit just yet. But I have a report that ranges from A:J I am trying to set up if conditions for conditional formatting so that if F2 = G2 change to green J2 fills in with "NO ACTION", if G2<F2 J2 fills in with "NO action" change to green, if G2 > F2 change color to yellow or red J2 fills in with "action required". Then for loop around so that it evaluates all F and G to the last row and fills in the corresponding J with the correct message.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I'm not clear which cells you want to change using CF, but I'll assume the Col J values for an example. Conditional formatting can handle the formatting end, e.g. "change to green" if F2=G2. But you will need a different mechanism, usually a formula to change the cell data, say to "NO ACTION"

Book9
ABCDEFGHIJKLM
1Data Col 1Data Col 2Data Col 3Data Col 4Data Col 5Data Col 6Data Col 7Data Col 8Data Col 9Data Col 10Data Col 11Data Col 12Data Col 13
2nanninosenurrytreadalbertinabedewoman240240vehicleutraquistNO ACTIONvagasnoninjuryvitta
3chuponrearsreshuffleguahivomolding133333depsidesthairmACTION REQUIREDfiretowerranariumapproach
4bounmiriamnemisstopsliberalaerostat186101bammedcaymansNO ACTIONolegdirdforloin
5pinkrootoutlearnseverlythegndomidahoans101277intersectcallantsACTION REQUIREDglotticbaguettesdialonian
6polynesiaendingslandracechampacsgyrodyne274165glochidstourneysNO ACTIONreformatsnudestmarjorams
7imaginatelettingcalzoonsresumeswittawer106260perfumerleistersACTION REQUIREDforefacesemanatingremolds
8vanaspatibakongofingentsarnasimeon254147amorinoornationNO ACTIONarbuteswinterizeisostere
9recededsharpenscraichedsalariegotahkhana283286pantilesdescendACTION REQUIREDtelurgyraphaelsimeon
10resituatetopcastlecallantscodolforefaces111234writhledguahivoACTION REQUIREDsibnessskinkingevoke
Sheet1
Cell Formulas
RangeFormula
J2:J10J2=IF(G2<=F2,"NO ACTION",IF(G2>F2,"ACTION REQUIRED"))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
J2:J10Cell Valuebeginning with "ACTION REQUIRED"textNO
J2:J10Cell Valuecontains "NO ACTION"textNO
 
Upvote 0
I'm not clear which cells you want to change using CF, but I'll assume the Col J values for an example. Conditional formatting can handle the formatting end, e.g. "change to green" if F2=G2. But you will need a different mechanism, usually a formula to change the cell data, say to "NO ACTION"

Book9
ABCDEFGHIJKLM
1Data Col 1Data Col 2Data Col 3Data Col 4Data Col 5Data Col 6Data Col 7Data Col 8Data Col 9Data Col 10Data Col 11Data Col 12Data Col 13
2nanninosenurrytreadalbertinabedewoman240240vehicleutraquistNO ACTIONvagasnoninjuryvitta
3chuponrearsreshuffleguahivomolding133333depsidesthairmACTION REQUIREDfiretowerranariumapproach
4bounmiriamnemisstopsliberalaerostat186101bammedcaymansNO ACTIONolegdirdforloin
5pinkrootoutlearnseverlythegndomidahoans101277intersectcallantsACTION REQUIREDglotticbaguettesdialonian
6polynesiaendingslandracechampacsgyrodyne274165glochidstourneysNO ACTIONreformatsnudestmarjorams
7imaginatelettingcalzoonsresumeswittawer106260perfumerleistersACTION REQUIREDforefacesemanatingremolds
8vanaspatibakongofingentsarnasimeon254147amorinoornationNO ACTIONarbuteswinterizeisostere
9recededsharpenscraichedsalariegotahkhana283286pantilesdescendACTION REQUIREDtelurgyraphaelsimeon
10resituatetopcastlecallantscodolforefaces111234writhledguahivoACTION REQUIREDsibnessskinkingevoke
Sheet1
Cell Formulas
RangeFormula
J2:J10J2=IF(G2<=F2,"NO ACTION",IF(G2>F2,"ACTION REQUIRED"))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
J2:J10Cell Valuebeginning with "ACTION REQUIRED"textNO
J2:J10Cell Valuecontains "NO ACTION"textNO
how do you relay that into vba code though? I have not really found good resources that explain converting from a formula in a cell into VBA code or how to run the conditional formatting from the normal formula into VBA Code
 
Upvote 0
The simplest way to to use the macro recorder to record yourself creating the conditional formatting rules that you want, then study the recorder-produced VBA code and apply it to whatever macro you are working on.
 
Upvote 0
Perhaps something like this for VBA in the sheet module:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LRow As Long, i As Long

LRow = Range("F" & Rows.Count).End(xlUp).Row

Application.EnableEvents = False
If Not Intersect(Target, Range("F2:G" & LRow)) Is Nothing Then
    For i = 2 To LRow
        If Range("F" & i).Value = Range("G" & i).Value Or Range("F" & i).Value > Range("G" & i).Value Then Range("J" & i).Interior.Color = vbGreen: Range("J" & i).Value = "NO ACTION"
        If Range("F" & i).Value < Range("G" & i).Value Then Range("J" & i).Interior.Color = vbYellow: Range("J" & i).Value = "ACTION REQUIRED"
    Next i
End If
Application.EnableEvents = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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