Loop through rows in range

hughhuyton

New Member
Joined
Dec 11, 2016
Messages
16
I have this code that runs through each cell of a range and then highlights the highest value in yellow. How would I incorporate a loop to have it loop through each row of the range for it to highlight the maximum value in each row.

Code:
<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545; min-height: 14.0px}</style>Private Sub HighestValue()
Dim cell As Range




  For Each cell In ActiveSheet.Range("A1:U1")
    If cell.Value = Application.WorksheetFunction.Max(Range("A1:U1")) Then
      cell.Interior.ColorIndex = 6
    Else
      cell.Interior.ColorIndex = xlNone
    End If
  Next
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Tim_Excel_

Well-known Member
Joined
Jul 12, 2016
Messages
512
Code:
Sub HiLighter()
    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
                GoTo getaway
            End If
        Next rr
getaway:
    Next r
End Sub

Thanks to Excel VBA: Excel VBA: Find the maximum value in each row and highlight it - Stack Overflow

Just make sure you change the values to your liking
 
Last edited:
Upvote 0

Forum statistics

Threads
1,191,687
Messages
5,988,077
Members
440,125
Latest member
vincentchu2369

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
Top