using vba for conditional formatting

QuizToon

New Member
Joined
Jan 31, 2009
Messages
28
Hi all,

I have a table with 12 columns. I would like to highlight the highest and lowest entry in each column. Highest in green and the lowest in red.

there is a lot of copying and pasting which I find messes up the standard method so was wondering if VBA would work better

Many Thanks
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
This code will do the trick. There are different ways to set the range of your data table that might be better than 'CurrentRegion', but this should work. This will also take into account the possibility of having duplicate Min and Max values in a column.

Code:
Sub test()
Dim R As Range: Set R = Range("A1").CurrentRegion 'Change to first cell of where your data is
Dim AR As Variant: AR = R.Value
Dim TMP As Variant
Dim Low As Long
Dim High As Long


For i = 1 To UBound(AR, 2)
    TMP = Application.Index(AR, , i)
    Low = Application.WorksheetFunction.Min(TMP)
    High = Application.WorksheetFunction.Max(TMP)
        For j = 1 To UBound(TMP)
            If TMP(j, 1) = Low Then R.Cells(j, i).Interior.ColorIndex = 4
            If TMP(j, 1) = High Then R.Cells(j, i).Interior.ColorIndex = 3
        Next j
Next i


End Sub
 
Upvote 0
This code will do the trick. There are different ways to set the range of your data table that might be better than 'CurrentRegion', but this should work. This will also take into account the possibility of having duplicate Min and Max values in a column.

Code:
Sub test()
Dim R As Range: Set R = Range("A1").CurrentRegion 'Change to first cell of where your data is
Dim AR As Variant: AR = R.Value
Dim TMP As Variant
Dim Low As Long
Dim High As Long


For i = 1 To UBound(AR, 2)
    TMP = Application.Index(AR, , i)
    Low = Application.WorksheetFunction.Min(TMP)
    High = Application.WorksheetFunction.Max(TMP)
        For j = 1 To UBound(TMP)
            If TMP(j, 1) = Low Then R.Cells(j, i).Interior.ColorIndex = 4
            If TMP(j, 1) = High Then R.Cells(j, i).Interior.ColorIndex = 3
        Next j
Next i


End Sub



This didn't work - nothing happened
 
Upvote 0
I assume the answer in post 3 will work for you. No more help from me needed.
You said in your original post:
I have a table with 12 columns

I asked for the Table name not the sheet name.

A Table and a sheet are not the same.


Thanks for the reply

The sheet is called 2018
 
Upvote 0
I assume the answer in post 3 will work for you. No more help from me needed.
You said in your original post:
I have a table with 12 columns

I asked for the Table name not the sheet name.

A Table and a sheet are not the same.


Sorry I did not realise.

The sheet is called 2018. In the sheet there 12 columns starting from cell C6 and running to N17
 
Upvote 0
How about
Code:
Sub MaxMinHilite()
   Dim i As Long
   For i = 3 To 14
      With Columns(i)
         Cells(Evaluate("match(max(" & .Address & ")," & .Address & ",0)"), i).Interior.Color = vbGreen
         Cells(Evaluate("match(min(" & .Address & ")," & .Address & ",0)"), i).Interior.Color = vbRed
      End With
   Next i
End Sub

EDIT:
If you have multiple max or min numbers in a column this will only highlight the first
 
Last edited:
Upvote 0
This didn't work - nothing happened

Did you read the code note and change where your data is? My code assumed it started in A1.

Now that I know where your data actually is, try this code.

Code:
Sub test()
Dim R As Range: Set R = Range("C6").CurrentRegion 'Change to first cell of where your data is
Dim AR As Variant: AR = R.Value
Dim TMP As Variant
Dim Low As Long
Dim High As Long


For i = 1 To UBound(AR, 2)
    TMP = Application.Index(AR, , i)
    Low = Application.WorksheetFunction.Min(TMP)
    High = Application.WorksheetFunction.Max(TMP)
        For j = 1 To UBound(TMP)
            If TMP(j, 1) = Low Then R.Cells(j, i).Interior.ColorIndex = 4
            If TMP(j, 1) = High Then R.Cells(j, i).Interior.ColorIndex = 3
        Next j
Next i


End Sub
 
Upvote 0
Did you read the code note and change where your data is? My code assumed it started in A1.

Now that I know where your data actually is, try this code.

Code:
Sub test()
Dim R As Range: Set R = Range("C6").CurrentRegion 'Change to first cell of where your data is
Dim AR As Variant: AR = R.Value
Dim TMP As Variant
Dim Low As Long
Dim High As Long


For i = 1 To UBound(AR, 2)
    TMP = Application.Index(AR, , i)
    Low = Application.WorksheetFunction.Min(TMP)
    High = Application.WorksheetFunction.Max(TMP)
        For j = 1 To UBound(TMP)
            If TMP(j, 1) = Low Then R.Cells(j, i).Interior.ColorIndex = 4
            If TMP(j, 1) = High Then R.Cells(j, i).Interior.ColorIndex = 3
        Next j
Next i


End Sub

HI,

Yes I did change the cell reference, and I followed your code notes, but still nothing.

I also tried the code from the helper above and those didn’twork either. I a guessing there issomething wrong with my spreadsheet
Thanks for trying though, I suppose I will just have to usethe standard method and amend each time it changes


Regards all
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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