简体   繁体   中英

Sorting VLOOKUP results in multiple columns

Good afternoon. I wonder if there is anyone here who can help me with a problem I can't fix and can't find a solution for online.

I have a spreadsheet which contains two worksheets (main and data) and a lot of VLOOKUP references, all based on a selection from a drop down list (cell D5).

The part I'm interested in is on how to sort some of the resultant data. I've set up part of the main worksheet to look like a table, which comprises two columns of cells (C8:C20 and D8:D20). Cells C8:C20 contain text (typed in, not lookup text), cells D8:D20 contain numbers, which are lookup numbers from the data worksheet. I want C8:C20 and D8:D20 to be sorted based on the numerica values returned from the VLOOKUP in D8:D20, which will of course change each time the lookup variable in D5 changes.

For example: D5 shows Council A. The cells in C8:C20 are parties which may comprise the affiliation of sitting councillors, D8:D20 are the number of councillors affiliated to each party. I want both C8:C20 to and D8:D20 to be organised most to least numbers of councillors:

PartyA - 10
PartyB - 8
PartyC - 3
PartyD - 0
PartyE - 0

And so on. When D5 is changed to Council B, then of course the numbers in D8:D20 will change, so I would want that result to show as:

PartyB - 15
PartyC - 12
PartyA - 9
PartyE - 5
PartyD - 3

etc.

Any ideas? Apologies if I'm not making my conundrum clear!

To directly sort the C8:D8 range you would need to configure a macro, possibly triggered by changes in the D5 dropdown.

A more maintainable alternative would create a visualization area, that automatically generates a sorted table. You can do that creating a helper column ( F , in this example), with this formula in F8 : =COUNTIF($D$8:$D$20;"<="&D8) , extending until the end of your data. This would return an index for each row.

Based on that, you can use

G8 =INDEX(C$8:C$20;MATCH(ROW()-7;$F$8:$F$20;0))

and

H8 =INDEX(D$8:D$20;MATCH(ROW()-7;$F$8:$F$20;0))

Populate the remaining rows with the same formula.

I'm using INDEX/MATCH instead of VLOOKUP here (and you should too!).

ROW()-7 returns the desired row in the table.

You can create a pivot table and set the sort according to column B. Then refresh when changes are made or automatically refresh when the table is opened Right Click any cell in the pivot table Click PivotTable Options Check the "Refresh Data When Opening the File"

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM