Highlight Lowest Sales VBA

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Hi

Here I am looking to highlight (the entire row) the lowest sales by region in VBA.

Thanks

IDRegionSales
100East1000
102East1100
103East1200
104West1000
105West1100
106West1200
107North1000
108North1100
109North1200
110South1000
111South1100
112South1200
113Pacific100
114Pacific200
115Pacific300

<tbody>
</tbody>
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You can use conditional formatting instead of VBA.

Select your range assuming data starts in A1 then select A1:C16 and in conditional formatting use this formula.

Code:
=MIN($C$2:$C$16)=$C2


Note: If there are ties for min sales this will highlight all of them.
 
Last edited:
Upvote 0
If you must have VBA then this will highlight the row automatically when the value in column C is changed. This must go into the sheet module. Right click on the sheet tab and select view code and past the code there. The file must be saved as a macro enabled file type like .xlsm

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 3 Then
    Dim lr As Long
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    
    Range("A2:C" & lr).Interior.Color = xlNone
    
    
    For x = 2 To lr
        If Cells(x, 3) = Application.WorksheetFunction.Min(Range("C2:C" & lr)) Then
            Range("A" & x & ":C" & x).Interior.ColorIndex = 6
        End If
    Next x


End If


End Sub
 
Upvote 0
It is necessary for this code to go into a private sub. I have other code which would precede this code.
 
Upvote 0
I do not think it has to, but that is what Excel defaults to when you create a worksheet change macro. It can also be modified to run when you run your other code if you like instead of a change event.
 
Upvote 0
I thought he wanted the lowest sales for each region.

So in this example there would be 4 rows highlighted.
One for each region.

And you can have more then one sheet change event script in your worksheet.
 
Last edited:
Upvote 0
Somehow missed that but this should work.

Code:
Sub hlmin()

Application.ScreenUpdating = False
Dim lr As Long
Dim ary() As Variant
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:C" & lr).Interior.ColorIndex = xlNone
mycount = lr - 1
ReDim ary(mycount, 1)

For x = 2 To lr
    For a = 0 To mycount
        ary(a, 0) = Cells(a + 2, 2)
        If Cells(x, 2) = Cells(a + 2, 2) Then
            ary(a, 1) = Cells(a + 2, 3)
        Else
            ary(a, 1) = "no"
        End If
    Next a
    mymin = WorksheetFunction.Min(ary)
    If Cells(x, 3) = mymin Then Range("A" & x & ":C" & x).Interior.ColorIndex = 6
    
Next x
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,930
Members
449,479
Latest member
nana abanyin

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