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

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Region[/TD]
[TD]Sales[/TD]
[/TR]
[TR]
[TD]100[/TD]
[TD]East[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]102[/TD]
[TD]East[/TD]
[TD]1100[/TD]
[/TR]
[TR]
[TD]103[/TD]
[TD]East[/TD]
[TD]1200[/TD]
[/TR]
[TR]
[TD]104[/TD]
[TD]West[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]105[/TD]
[TD]West[/TD]
[TD]1100[/TD]
[/TR]
[TR]
[TD]106[/TD]
[TD]West[/TD]
[TD]1200[/TD]
[/TR]
[TR]
[TD]107[/TD]
[TD]North[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]108[/TD]
[TD]North[/TD]
[TD]1100[/TD]
[/TR]
[TR]
[TD]109[/TD]
[TD]North[/TD]
[TD]1200[/TD]
[/TR]
[TR]
[TD]110[/TD]
[TD]South[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]111[/TD]
[TD]South[/TD]
[TD]1100[/TD]
[/TR]
[TR]
[TD]112[/TD]
[TD]South[/TD]
[TD]1200[/TD]
[/TR]
[TR]
[TD]113[/TD]
[TD]Pacific[/TD]
[TD]100[/TD]
[/TR]
[TR]
[TD]114[/TD]
[TD]Pacific[/TD]
[TD]200[/TD]
[/TR]
[TR]
[TD]115[/TD]
[TD]Pacific[/TD]
[TD]300[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
This find the last row with data using column A (column 1)
Code:
[COLOR=#333333]lr = Cells(Rows.Count, 1).End(xlUp).Row[/COLOR]

This clears the color fill so that if there is a new min the old min will not still be highlighted
Code:
Range("A2:C" & lr).Interior.ColorIndex = xlNone

Mycount is used to define how many rows the array has. ary is the array. ary was declared at the start with out any dimensions here it is re-declared with with dimensions.
Code:
mycount = lr - 1
ReDim ary(mycount, 1)


This sections has two loops one to go though each row of data in the sheet, the For x=2 to lr. and a loop to put the values in the array. The if statement checks if the region is the same as the current row, that is row x. If so it put the sales number into the array if not it puts text. Then a min is done on the array since min ignores the text it will find the min for that region.
Then it checks if the value on that row is equal to the min and if so colors the cells.
Code:
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

By turning off screen updating the code runs a little faster. False turns off screen updating and true turns it back on.
Code:
Application.ScreenUpdating = False
Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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