Home

How to return multiple results based on multiple criteria

%3CLINGO-SUB%20id%3D%22lingo-sub-339882%22%20slang%3D%22en-US%22%3EHow%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-339882%22%20slang%3D%22en-US%22%3E%3CP%3EI'd%20like%20to%20return%20data%20from%20multiple%20worksheets%20based%20upon%20multiple%20criteria.%26nbsp%3B%20Here%20is%20an%20example%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3EData%20Table%201%20(in%20worksheet%201)%3C%2FSTRONG%3E%3C%2FP%3E%3CTABLE%20cellspacing%3D%220%22%20cellpadding%3D%220%22%20border%3D%220%22%3E%3CTBODY%3E%3CTR%3E%3CTD%3EItem%3C%2FTD%3E%3CTD%3ELocation%3C%2FTD%3E%3CTD%3EQty%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Ethis%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E5%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Ethat%3C%2FTD%3E%3CTD%3Ewest%3C%2FTD%3E%3CTD%3E10%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Eanother%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E15%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%3CEM%3ENote%3A%26nbsp%3B%20The%20table%20is%20dynamic.%26nbsp%3B%20Items%20may%20be%20both%20entered%20and%20removed.%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EData%20Table%202%20(in%20worksheet%202)%3C%2FSTRONG%3E%3C%2FP%3E%3CTABLE%20cellspacing%3D%220%22%20cellpadding%3D%220%22%20border%3D%220%22%3E%3CTBODY%3E%3CTR%3E%3CTD%3EItem%3C%2FTD%3E%3CTD%3ELocation%3C%2FTD%3E%3CTD%3EQty%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Eknife%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E5%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Espoon%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E10%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Efork%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E15%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%3CEM%3ENote%3A%26nbsp%3B%20The%20table%20is%20dynamic.%26nbsp%3B%20Items%20may%20be%20both%20entered%20and%20removed.%3C%2FEM%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ECriteria%20(in%20worksheet%203%20above%20the%20data%20returned)%3C%2FSTRONG%3E%3C%2FP%3E%3CTABLE%20cellspacing%3D%220%22%20cellpadding%3D%220%22%20border%3D%220%22%3E%3CTBODY%3E%3CTR%3E%3CTD%3EItem%3C%2FTD%3E%3CTD%3ELocation%3C%2FTD%3E%3CTD%3EQty%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3E%26nbsp%3B%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E%26gt%3B10%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EData%20Returned%20(in%20worksheet%203)%3C%2FSTRONG%3E%3C%2FP%3E%3CTABLE%3E%3CTBODY%3E%3CTR%3E%3CTD%3EItem%3C%2FTD%3E%3CTD%3ELocation%3C%2FTD%3E%3CTD%3EQty%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Eanother%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E15%3C%2FTD%3E%3C%2FTR%3E%3CTR%3E%3CTD%3Efork%3C%2FTD%3E%3CTD%3Eeast%3C%2FTD%3E%3CTD%3E15%3C%2FTD%3E%3C%2FTR%3E%3C%2FTBODY%3E%3C%2FTABLE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20do%20this%20using%20the%20Data%20Tab%20%2F%20Sort%20and%20Filter%20%2F%20Advance%20Filter%20function%20when%20there%20is%20only%20one%20master%20data%20table.%26nbsp%3B%20But%20I%20don't%20know%20a%20way%20to%20do%20it%20when%20I%20may%20have%2010%20or%2020%20different%20data%20tables%20with%20each%20in%20its%20own%20worksheet.%26nbsp%3B%20I'm%20not%20adverse%20to%20using%20cheater%20columns%2C%20and%2For%20cheater%20worksheets.%26nbsp%3B%20I'll%20use%20a%20macro%2C%20if%20needed%2C%20but%20I'd%20prefer%20to%20avoid%20doing%20so.%26nbsp%3B%20I've%20thought%20about%20using%20a%20macro%20to%20copy%20all%20the%20data%20from%20the%20various%20worksheets%20into%20a%20hidden%20cheater%20worksheet%20and%20then%20using%20the%20Data%20Tab%20%2F%20Sort%20and%20Filter%20%2F%20Advance%20Filter%20function%20to%20query%20the%20data%2C%20but%20I'm%20wondering%20if%20there%20is%20a%20more%20elegant%20way%20(and%20one%20that%20avoids%20using%20a%20macro).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20guidance%20will%20be%20greatly%20appreciated.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance%2C%3C%2FP%3E%3CP%3EAndrew%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-339882%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-353479%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-353479%22%20slang%3D%22en-US%22%3EIf%20possible%2C%20avoid%20the%20volatile%20INDIRECT.%20Use%20CHOOSE%20instead%2C%20as%20discussed%20here%3A%3CBR%20%2F%3E%3CA%20href%3D%22http%3A%2F%2Fwww.decisionmodels.com%2Foptspeedb.htm%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.decisionmodels.com%2Foptspeedb.htm%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-353449%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-353449%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20Twifoo.%26nbsp%3B%20That%20is%20what%20I%20had%20done%20and%20I%20have%20a%20solid%20grasp%20of%20what%20you%20did.%26nbsp%3B%20Using%20an%20index%20within%20a%20named%20range%20was%20a%20brilliant%20idea.%26nbsp%3B%20I%20would%20never%20have%20thought%20of%20that%20on%20my%20own.%26nbsp%3B%20Now%20I%20need%20to%20check%20if%20the%20worksheet%20formulas%20work%20when%20we%20load%20all%20the%20locations.%26nbsp%3B%20(If%20needed%2C%20I%20may%20be%20able%20to%20use%20some%20cheater%20cells%20with%20the%20INDIRECT%20function.%26nbsp%3B%20But%20I%20don't%20think%20it's%20going%20to%20be%20a%20problem.)%26nbsp%3B%20We're%20also%20chasing%20exception%20handling%3B%20such%20as%2C%20a%20user%20entering%20the%20same%20component%20twice.%26nbsp%3B%20The%20example%20I%20provided%20was%20very%20simple%20compared%20to%20the%20actual%20data%20that%20will%20be%20entered%20and%20results%20that%20will%20need%20to%20be%20returned.%26nbsp%3B%20But%20so%20far%2C%20everything%20is%20working%20splendidly.%26nbsp%3B%20Thanks%20again.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-353250%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-353250%22%20slang%3D%22en-US%22%3EFocus%20your%20attention%20on%20Sheets%204%20and%205%20of%20the%20file%20I%20attached%20earlier.%20Then%2C%20update%20me%20of%20your%20thoughts%20thereon.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-352758%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-352758%22%20slang%3D%22en-US%22%3E%3CP%3ESergei%2C%20thank%20you.%26nbsp%3B%20I%20will%20check%20on%20this.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-352749%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-352749%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Andrew%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMore%20exactly%20they%20won't%20be%20able%20to%20update%20the%20query.%20You%20may%20check%20where%20PQ%20is%20available%20here%26nbsp%3B%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CA%20href%3D%22https%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2Fwhere-is-get-transform-power-query-e9332067-8e49-46fc-97ff-f2e1bfa0cb16%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2Fwhere-is-get-transform-power-query-e9332067-8e49-46fc-97ff-f2e1bfa0cb16%3C%2FA%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-352690%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-352690%22%20slang%3D%22en-US%22%3E%3CP%3ETwifoo%2C%20thank%20you!%20My%20apologies%20for%20not%20responding%20sooner.%20I%20was%20out%20on%20PTO%20the%20last%20half%20of%20last%20week.%20Your%20solution%20will%20do%20exactly%20as%20we%20need.%20I've%20taken%20a%20little%20time%20this%20morning%20to%20examine%20how%20you%20accomplished%20the%20task.%20It's%20quite%20elegant.%20But%20as%20you%20mentioned%2C%20by%20the%20time%20we%20add%20in%20several%20criteria%20and%2020-ish%20locations%20then%20Excel%20may%20have%20an%20issue%20either%20with%20the%20formula%20or%20in%20performance.%20I%20haven't%20tested%20that%20yet.%20But%20I%20will%20and%20I'll%20get%20back%20to%20you%20on%20how%20well%20it%20works.%3C%2FP%3E%3CP%3EThanks%20again%2C%3CBR%20%2F%3EAndrew%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-352689%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-352689%22%20slang%3D%22en-US%22%3E%3CP%3ESergei%2C%20Power%20Query%20may%20be%20the%20way%20for%20us%20to%20go.%20I%20see%20it%20will%20perform%20the%20functions%20we%20need.%20However%2C%20although%20our%20company%20has%20access%20to%20Power%20BI%20through%20our%20Office%20365%20for%20Business%20subscription%2C%20I'm%20not%20sure%20that%20our%20client%20has%20access.%20I%20haven't%20reviewed%20the%20material%20on%20the%20functionality%2C%20but%20if%20I%20understand%20correctly%20workbooks%20we%20create%20using%20the%20Power%20Query%20functionality%20won't%20be%20able%20to%20be%20used%20by%20another%20unless%20they%20also%20have%20an%20Office%20subscription%20that%20contains%20the%20functionality.%20Is%20this%20correct%3F%3C%2FP%3E%3CP%3EThanks%20again%2C%3CBR%20%2F%3EAndrew%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-341138%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-341138%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20Andrew%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESheet3%20of%20the%20attached%20file%20returns%20the%20filtered%20data%2C%20as%20you%20specified.%20Note%20that%20the%20formulas%20in%20Sheet3%20must%20be%20accordingly%20adjusted%20as%20more%20data%20tables%20are%20added%2C%20thereby%20resulting%20in%20an%20extremely%20long%20formula%20that%20may%20cause%20Excel%20to%20crash!%3C%2FP%3E%3CP%3ENonetheless%2C%20I%20suggest%20that%20instead%20of%20adding%20more%20data%20tables%2C%20data%20should%20be%20combined%20into%20only%20one%20table%2C%20as%20shown%20in%20Sheet4%2C%20so%20that%20filtered%20data%20can%20be%20returned%2C%20with%20automatically%20sorted%20items%2C%20as%20shown%20in%20Sheet5.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20eschewed%20volatile%20functions%20in%20the%20formulas%20therein.%3C%2FP%3E%3CP%3ECheers!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETwifoo%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-339967%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20return%20multiple%20results%20based%20on%20multiple%20criteria%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-339967%22%20slang%3D%22en-US%22%3E%3CP%3EInstead%20of%20macro%20that%20could%20be%20done%20by%20Power%20Query%20-%20query%20all%20your%20tables%2C%20combine%20them%2C%20filter%20on%20criteria%20and%20land%20the%20result%20back%20to%20the%20sheet.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Andrew Lindsay
Occasional Contributor

I'd like to return data from multiple worksheets based upon multiple criteria.  Here is an example:

Data Table 1 (in worksheet 1)

ItemLocationQty
thiseast5
thatwest10
anothereast15

Note:  The table is dynamic.  Items may be both entered and removed.

 

Data Table 2 (in worksheet 2)

ItemLocationQty
knifeeast5
spooneast10
forkeast15

Note:  The table is dynamic.  Items may be both entered and removed.

 

Criteria (in worksheet 3 above the data returned)

ItemLocationQty
 east>10

 

Data Returned (in worksheet 3)

ItemLocationQty
anothereast15
forkeast15

 

I can do this using the Data Tab / Sort and Filter / Advance Filter function when there is only one master data table.  But I don't know a way to do it when I may have 10 or 20 different data tables with each in its own worksheet.  I'm not adverse to using cheater columns, and/or cheater worksheets.  I'll use a macro, if needed, but I'd prefer to avoid doing so.  I've thought about using a macro to copy all the data from the various worksheets into a hidden cheater worksheet and then using the Data Tab / Sort and Filter / Advance Filter function to query the data, but I'm wondering if there is a more elegant way (and one that avoids using a macro).

 

Any guidance will be greatly appreciated.

 

Thanks in advance,

Andrew

9 Replies

Instead of macro that could be done by Power Query - query all your tables, combine them, filter on criteria and land the result back to the sheet.

Hello Andrew, 

 

Sheet3 of the attached file returns the filtered data, as you specified. Note that the formulas in Sheet3 must be accordingly adjusted as more data tables are added, thereby resulting in an extremely long formula that may cause Excel to crash!

Nonetheless, I suggest that instead of adding more data tables, data should be combined into only one table, as shown in Sheet4, so that filtered data can be returned, with automatically sorted items, as shown in Sheet5. 

I eschewed volatile functions in the formulas therein.

Cheers!

 

Twifoo

Sergei, Power Query may be the way for us to go. I see it will perform the functions we need. However, although our company has access to Power BI through our Office 365 for Business subscription, I'm not sure that our client has access. I haven't reviewed the material on the functionality, but if I understand correctly workbooks we create using the Power Query functionality won't be able to be used by another unless they also have an Office subscription that contains the functionality. Is this correct?

Thanks again,
Andrew

Twifoo, thank you! My apologies for not responding sooner. I was out on PTO the last half of last week. Your solution will do exactly as we need. I've taken a little time this morning to examine how you accomplished the task. It's quite elegant. But as you mentioned, by the time we add in several criteria and 20-ish locations then Excel may have an issue either with the formula or in performance. I haven't tested that yet. But I will and I'll get back to you on how well it works.

Thanks again,
Andrew

Hi Andrew,

 

More exactly they won't be able to update the query. You may check where PQ is available here https://support.office.com/en-us/article/where-is-get-transform-power-query-e9332067-8e49-46fc-97ff-...

Sergei, thank you.  I will check on this.

Focus your attention on Sheets 4 and 5 of the file I attached earlier. Then, update me of your thoughts thereon.

Thanks Twifoo.  That is what I had done and I have a solid grasp of what you did.  Using an index within a named range was a brilliant idea.  I would never have thought of that on my own.  Now I need to check if the worksheet formulas work when we load all the locations.  (If needed, I may be able to use some cheater cells with the INDIRECT function.  But I don't think it's going to be a problem.)  We're also chasing exception handling; such as, a user entering the same component twice.  The example I provided was very simple compared to the actual data that will be entered and results that will need to be returned.  But so far, everything is working splendidly.  Thanks again.

If possible, avoid the volatile INDIRECT. Use CHOOSE instead, as discussed here:
http://www.decisionmodels.com/optspeedb.htm
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
23 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
14 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies