VBA to highlight Top N Values in Rows Based on Specific Range

chingching831

New Member
Joined
Jun 2, 2022
Messages
32
Office Version
  1. 2019
Platform
  1. Windows
Hi there,

I would like to use VBA code to highlight the highest values (cell in yellow, font in bold and red) of each row based on B2:G21.
May I know how the below VBA can be revised in order to achieve the expected result shown below?

1726543348153.png


VBA Code:
Sub Highlight_Max_Values_in_Rows()
    ActiveSheet.UsedRange
    Dim rA As Range, r, wf As WorksheetFunction
    Dim V As Variant, RWW As Range, rr As Range
    Set rA = Intersect(Range("A:A"), ActiveSheet.UsedRange)
    Set wf = Application.WorksheetFunction
    For Each r In rA
        Set RWW = Intersect(r.EntireRow, ActiveSheet.UsedRange)
        If wf.CountA(RWW) = 0 Then Exit Sub
        V = wf.Max(RWW)
        For Each rr In RWW
            If rr.Value = V Then
                rr.Interior.ColorIndex = 6
                rr.Font.Bold = True
                rr.Font.ColorIndex = 3
                GoTo getaway
            End If
        Next rr
getaway:
    Next r
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi @chingching831.
Try the following modified code:
VBA Code:
Option Explicit

Sub Highlight_Max_Values_in_Rows()
    Dim r           As Variant

    With Range("B2:G22")
        .ClearFormats
        .Font.Bold = False
        .Font.ColorIndex = xlAutomatic
        .Interior.ColorIndex = xlNone
    End With

    Dim rA          As Range
    Set rA = Range("B2:B22")    ' Change to the range you need

    For Each r In rA

        Dim RWW     As Range
        Set RWW = Range("B" & r.Row & ":G" & r.Row)
        If Application.WorksheetFunction.CountA(RWW) = 0 Then Exit Sub

        Dim V       As Variant
        V = Application.WorksheetFunction.Max(RWW)

        Dim rr      As Range

        For Each rr In RWW

            If rr.Value = V Then
                rr.Interior.Color = RGB(255, 255, 0)
                rr.Font.Bold = True
                rr.Font.Color = RGB(255, 0, 0)
                Exit For
            End If

        Next rr

    Next r

    Set RWW = Nothing
    Set rA = Nothing
End Sub
Maybe this is what you want to get as a result, if I understood you correctly. Good luck.
 
Upvote 0
Another option.
VBA Code:
Sub Highlight_Max_Values_in_Rows()
    Dim numarea As Range
    Dim c As Long, i As Long, maxcol As Long
    Dim maxval As Double
    Set numarea = Range("B2:G21")
    For i = 1 To numarea.Rows.Count
        maxval = numarea(i, 1)
        maxcol = 1
        For c = 2 To numarea.Columns.Count
            If numarea(i, c) > maxval Then
                maxval = numarea(i, c)
                maxcol = c
            End If
        Next c
        With numarea(i, maxcol)
            .Interior.ColorIndex = 6
            .Font.Bold = True
            .Font.ColorIndex = 3
        End With
    Next i
End Sub
 
Upvote 0
Solution
Another one :
VBA Code:
Sub Highlight_Max_Values_in_Rows()
Dim rng As Range, cel$, r&
Set rng = Range([A1], ActiveSheet.UsedRange)
With rng
    .Font.ColorIndex = xlAutomatic
    .Font.Bold = False
    .Interior.ColorIndex = xlNone
End With
For r = 1 To rng.Rows.Count
    If WorksheetFunction.CountA(Rows(r)) = 0 Then GoTo n
    cel = ActiveSheet.Rows(r).Find(WorksheetFunction.Max(ActiveSheet.Rows(r))).Address
    With Range(cel)
        .Interior.ColorIndex = 6
        .Font.Bold = True
        .Font.ColorIndex = 3
    End With
n: Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,357
Members
451,642
Latest member
mirofa

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