Kth largest element from multiple ranges

ChetShannon

Board Regular
Joined
Jul 27, 2007
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Anyone know how to create a formula for the kth largest element of say two different ranges? Preferably done via an Excel formula instead of VBA? In other words I have two ranges and I want to find the k-th largest element between those two ranges. I don't think the LARGE command will allow more than one range to my knowledge.
Thanks! Chet
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This should work if the ranges are on the same sheet:

Book1
ABCD
11
2328
354
476
598
610
712
814
Sheet6
Cell Formulas
RangeFormula
D2D2=LARGE((A1:A5,B2:B8),5)
 
Upvote 0
Unfortunately the two ranges are on two different sheets. :( Any other options in that case? Thx! Chet
 
Upvote 0
Or a slight variation of FormR's formula:

=AGGREGATE(14,6,IF({0,1},A1:A5,Sheet2!$G$1:$G$3),3)

k in both formulas is the last number. FormR's formula is easier to adapt for more ranges than 2.
 
Upvote 0
A bit clunky, but you could reference the range on the other sheet in a range on the sheet with the formulas and then include that range. Something like:
Book1
ABCDE
1Range on this sheet:Range from other sheet:
25113
3135
457
580
Sheet2
Cell Formulas
RangeFormula
A2A2=LARGE((C2:C5,E:E),5)
E2:E5E2=Sheet1!C2
 
Upvote 0
Thank you for the responses!... One follow-up question though is I see this formula from EricW of
=AGGREGATE(14,6,IF({0,1},A1:A5,Sheet2!$G$1:$G$3),3) but what is the IF{0,1} part of the formula for. I didn't follow that.. Thanks!
 
Upvote 0
Excel doesn't like ranges pointing to different sheets in some cases (like this one). But you can sometimes get around it by converting the ranges to internal arrays. FormR used CHOOSE to do that, I used IF. An IF statement has the basic form:

=IF(condition,TRUE result, FALSE result)

The condition part should return a TRUE/FALSE value, and based on that, the proper result is chosen. You can also use a number to return a TRUE/FALSE value. 0=FALSE, and any non-zero number=TRUE. And if you want to use multiple values, you can put them in an array using the {} braces. So this part of the formula

=IF({0,1},

is giving IF 2 values, a 0 and a 1, which are equivalent to FALSE and TRUE. So in essence, it's saying give me both the FALSE and the TRUE results. Then the 2 arrays are (more or less) intermingled, and the LARGE subfunction of AGGREGATE looks at the mingled array. The "ignore errors" option of AGGREGATE is required, since if the 2 ranges are different sizes, some elements of the arrays will be errors.

If you use CHOOSE, like FormR, you can extend his formula just by increasing the array constant like this:

=CHOOSE({1,2,3},Range1,Range2,Range3)
 
Upvote 0
An additional follow-up question. I am using the formula =AGGREGATE(15,6,CHOOSE({1,2},Sheet1!A1:A5,Sheet2!B2:B8),5) in order to find the SMALLest n-th items. The problem I am having is that the source ranges contain some valid times in contiguous sections (from top of range down n rows) and the rest of the lower cells are all zeros. The AGGREGATE formula is picking up the zero items as it should, but I don't want those zero data cells. I found out that if I make the range only to include the data that has actual times then it works. I've been trying to dynamically have the end of the range that has valid times in the formula but that isn't quite working. Is there some easier way to exclude the zero values when trying to find the n-th smallest items from multiple ranges?
 
Upvote 0
One quick way:

=AGGREGATE(15,6,1/(1/CHOOSE({1,2},Sheet1!A1:A5,Sheet2!B2:B8)),5)

This finds the reciprocal of the numbers. If 0, this creates a #DIV/0! error. Then it does the reciprocal again to restore the values for the non-0 numbers. Then AGGREGATE ignores the #DIV/0! errors.
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

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