Conditional Formating Excel Table

flipland

New Member
Joined
Mar 26, 2019
Messages
9
Hi. I would like to have conditional formatting done on an Excel Table based on the MIN and MAX values of every row in the table. Cells of the MIN values are to be highligted in RED with WHITE font. Celss of the MAX values are to be highlighted in GREEN with WHITE font.

I was able to find VBA code to conditionally format by row, but am stuck as to how to make it loop down to the next row. The code is copy-pasted below, as well as a snippet of the table. Would appreciate any help on this. Thanks!

CODE:
Public Sub MIN_MAX_Format()

ActiveSheet.Range("A1").Select
ActiveCell.Offset(1, 1).Select
Range("b2:r2").Select


Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition
Set rg = ActiveSheet.Range("B2:R2")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'define the rule for each conditional format
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$U2")
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$T2")

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

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

ActiveCell.Offset(1, 0).Select


End Sub



The table looks like this:

https://drive.google.com/file/d/1yTWNE9WaHTzBJRUfXZ_VKq5v8ZdJZdFf/view?usp=sharing

[TABLE="width: 1344"]
<tbody>[TR]
[TD]Country Name[/TD]
[TD]Bangladesh[/TD]
[TD]Bhutan[/TD]
[TD]China[/TD]
[TD]Hong Kong SAR, China[/TD]
[TD]Indonesia[/TD]
[TD]Japan[/TD]
[TD]Lao PDR[/TD]
[TD]Sri Lanka[/TD]
[TD]Macao SAR, China[/TD]
[TD]Myanmar[/TD]
[TD]Malaysia[/TD]
[TD]Nepal[/TD]
[TD]Philippines[/TD]
[TD]Singapore[/TD]
[TD]Thailand[/TD]
[TD]Vietnam[/TD]
[TD="colspan: 2"]Korea, Rep.[/TD]
[TD]MIN[/TD]
[TD]MAX[/TD]
[/TR]
[TR]
[TD]1960[/TD]
[TD]89[/TD]
[TD][/TD]
[TD]90[/TD]
[TD]429[/TD]
[TD][/TD]
[TD]479[/TD]
[TD][/TD]
[TD]143[/TD]
[TD][/TD]
[TD][/TD]
[TD]235[/TD]
[TD]51[/TD]
[TD]254[/TD]
[TD]428[/TD]
[TD]101[/TD]
[TD][/TD]
[TD]158[/TD]
[TD][/TD]
[TD]51[/TD]
[TD]479[/TD]
[/TR]
[TR]
[TD]1961[/TD]
[TD]97[/TD]
[TD][/TD]
[TD]76[/TD]
[TD]437[/TD]
[TD][/TD]
[TD]564[/TD]
[TD][/TD]
[TD]143[/TD]
[TD][/TD]
[TD][/TD]
[TD]226[/TD]
[TD]52[/TD]
[TD]267[/TD]
[TD]449[/TD]
[TD]107[/TD]
[TD][/TD]
[TD]94[/TD]
[TD][/TD]
[TD]52[/TD]
[TD]564[/TD]
[/TR]
[TR]
[TD]1962[/TD]
[TD]100[/TD]
[TD][/TD]
[TD]71[/TD]
[TD]488[/TD]
[TD][/TD]
[TD]634[/TD]
[TD][/TD]
[TD]139[/TD]
[TD][/TD]
[TD][/TD]
[TD]230[/TD]
[TD]55[/TD]
[TD]157[/TD]
[TD]472[/TD]
[TD]114[/TD]
[TD][/TD]
[TD]106[/TD]
[TD][/TD]
[TD]55[/TD]
[TD]634[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
How about
Code:
Sub flipland()
   With Range("A2", Range("A" & Rows.Count).End(xlUp).Offset(, 18))
      .FormatConditions.Delete
      .FormatConditions.Add xlExpression, , "=A2=MAX($B2:$R2)"
      .FormatConditions.Add xlExpression, , "=A2=MIN($B2:$R2)"
      With .FormatConditions(1)
         .Interior.Color = vbGreen
         .Font.Color = vbWhite
      End With
      With .FormatConditions(2)
         .Interior.Color = vbRed
         .Font.Color = vbWhite
      End With
   End With
End Sub
 
Upvote 0
Hi...

I think I got the loop part going using a FOR EACH LOOP. The first row of data now is being conditionally formatted. It then goes to the first cell of the second row, but it keeps formatting the first row of data. What am I missing here?

Rich (BB code):
Public Sub ForColorLoop()


    For Each rg In Selection
        MIN_MAX_Format

        'go to next row/range
        ActiveCell.Offset(1, 0).Select

    'go to next row or range
    Next rg

End Sub

Public Sub MIN_MAX_Format()

    Dim rg As Range
    Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition
    Set rg = ActiveSheet.Range("B2:R2")

    'clear any existing conditional formatting
    rg.FormatConditions.Delete

    'define the rule for each conditional format
    Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$U2")
    Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$T2")

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

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


End Sub
 
Upvote 0
Did you try my suggestion in post#2?
 
Upvote 0
THANK YOU! That is so much more elegant than what I was trying to with the second iteration I posted a few mins ago with the LOOP code. I'd still like to understand what I was missing with that one and would appreciate feedback on it. Re-posting it below...

Rich (BB code):
Public Sub ForColorLoop()


    For Each rg In Selection
        MIN_MAX_Format

        'go to next row/range
        ActiveCell.Offset(1, 0).Select

    'go to next row or range
    Next rg

End Sub

___________________

Public Sub MIN_MAX_Format()

    Dim rg As Range
    Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition
    Set rg = ActiveSheet.Range("B2:R2")

    'clear any existing conditional formatting
    rg.FormatConditions.Delete

    'define the rule for each conditional format
    Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$U2")
    Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, "=$T2")

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

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


End Sub
 
Upvote 0
You're MIN_MAX code is always looking at
Code:
[I]ActiveSheet.Range("B2:R2")[/I]
So it will only format that row.
Also even if that line was altered to look at the active cell row3 & below would not work properly, because they would still be looking at the value in U2 & T2
 
Upvote 0
Yes it can be made to work, but I don't see the point when the code I posted does the same thing, but without loops (which are slow).
Also with your code, rather than having 2 conditional format rules, you will have 2 for each & every row with data, which can slow things down.
 
Upvote 0
Yes it can be made to work, but I don't see the point when the code I posted does the same thing, but without loops (which are slow).
Also with your code, rather than having 2 conditional format rules, you will have 2 for each & every row with data, which can slow things down.


Most certainly your code is so much more efficient. Again, thank you very much!
I just wanted to know if, on the other one, I was at least on the right track and where it is that I am making an error.
Much appreciated!
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,580
Members
452,653
Latest member
craigje92

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