February 14, 2016

Highlight Minimum values in rows in Excel Worksheet with VBA

How to highlight minimum values in rows in Excel worksheet with VBA. Watch the training video:

You can watch this video on YouTube.

Here’s the complete VBA code:

Sub highlightMinValue()
Dim ws As Worksheet
Dim i As Long, lastrow As Long
Set ws = Worksheets(“Data”)
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Dim minvalue As Long
Dim cell As Range
Dim rng As Range
For i = 1 To lastrow
 ‘We loop through the rows
Set rng = Range(Cells(i, 1), Cells(i, 4))
minvalue = Application.WorksheetFunction.Min(rng)
For Each cell In rng
‘Now we loop through each cell in the identified row
If cell.Value = minvalue Then
cell.Font.Color = vbRed
End If
Next cell
Next i
End Sub