Formula/macro/VBA to match top 5 outstanding amounts to an offset name.

dstepan

Board Regular
Joined
Apr 16, 2009
Messages
160
Hello all. I have a head banger issue. Any and all help is a greatly appreciated.
This data is exported from an application.
I am working with Excel 2016.
My attachment shows the data I am working with. Obviously there are more columns (Col’s C:U) and many more rows.
I am trying to complete a 3 day project of extracting specific information from an aging report with macros..
The final and most difficult step is to find the top 5 most “Outstanding amounts” (Col B or Col V) with the associated “Customer Name” (Col A)
The first 2 columns show the actual exported data. I created column V to extract the $ amount. Formula: =if(a5=”Customer Totals:”,a5,0).
I don’t care if the information is highlighted or put in a table in another part of the spreadsheet (ex: Col’s Y and Z)
AB V
1
2
3
4GNPGNP
5INVL10466 -
6INVL10585 -
7INVL10698 -
8INVL10726 -
9INVL10734 -
10INVL10796 -
11Customer Totals:$324,711.00 324,711.00
12AMERICAAMERICA -
13INVL10792 -
14Customer Totals:$56,002.50 56,002.50
15TONANTONAN -
16INVL10713 -
17INVL10714 -
18Customer Totals:$75,489.00 75,489.00
19FLYBYFLYBY -
20INVL10715 -
21INVL10720 -
22INVL10756 -
23INVL10758 -
24Customer Totals:$290,946.70 290,946.70
25BASICBASIC -
26INVL10763 -
27Customer Totals:$114,986.50 114,986.50
28BAYBAY -
29INVL10716 -
30Customer Totals:$5,446.23 5,446.23
31BOWBOW -
32INVL10649 -
33Customer Totals:160449.25 160,449.25
34SOLSOL -
35INVL10746 -
36Customer Totals:$26,250.00 26,250.00
37CONSUCONSU -
38INVL10797 -
39INVL10788 -
40Customer Totals:$291,518.00 291,518.00
41COVANCOVAN -
42INVL10686 -
43INVL10789 -
44Customer Totals:$170,735.55 170,735.55
45DURGDURG -
46INVL10793 -
47INVL10800 -
48Customer Totals:$388,000.00 388,000.00
49ELCHELCH -
50INVL10769 -
51Customer Totals:$3,900.00 3,900.00
52EMERALEMERAL -
53INVL10700 -
54Customer Totals:$2,625.00 2,625.00
55EXENEXEN -
56INVL10778 -
57Customer Totals:$117,000.00 117,000.00

<colgroup><col><col><col><col><col><col span="2"></colgroup><tbody>
</tbody>
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
See if this does what you need

Code:
Sub aTest()
Dim lastRow As Long

'Get the last row with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
'Formula in Column V
Range("V4:V" & lastRow).Formula = "=IF(ISNUMBER(B4),B4,"""")"
'Formula in Column W
Range("W4:W" & lastRow).Formula = _
    "=IF(ISNUMBER(V4),LOOKUP(2,1/((A$4:A4<>""INV"")*(A$4:A4<>""Customer Totals:"")),A$4:A4),"""")"

'Headers
Range("X1:AA1") = Array("Exclude", "Large", "Value", "Customer")
'Exclusion list
Range("X2:X5") = Application.Transpose(Array("Gmit", "BG5", "GRM", "Gmit SC"))
'Large
Range("Y2:Y6") = Application.Transpose(Array(1, 2, 3, 4, 5))

'Formulas to get top 5 values
With Range("Z2:Z6")
    .Formula = _
        "=AGGREGATE(14,6,V$4:V$" & lastRow & "/ISNA(MATCH(W$4:W$" & lastRow & ",X$2:X$5,0)),Y2)"
    .NumberFormat = "#,##0.00"
End With
'Formulas to get top 5 customers
Range("AA2:AA6").Formula = "=INDEX(W$4:W$" & lastRow & ",MATCH(Z2,V$4:V$" & lastRow & ",0))"
    
End Sub

M.
 
Upvote 0
Awesome!!

How can I change the formula for column V and W so as not to not pick up the last row?

The last row has "Report Totals" with $ amounts in columns B:F.

The code is picking this up and using it as if it were a customer. It is showing as the number 1 customer.

Other than that this works like a charm!!!
 
Upvote 0
Change

'Get the last row with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
to
'Get the last row with data
lastRow = Cells(Rows.Count, "A").End(xlUp).Row - 1

M.

 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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