Dynamically look up values in sheets based on sheet name

%3CLINGO-SUB%20id%3D%22lingo-sub-2576512%22%20slang%3D%22en-US%22%3EDynamically%20look%20up%20values%20in%20sheets%20based%20on%20sheet%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2576512%22%20slang%3D%22en-US%22%3E%3CP%3EWindows%2010%2C%20Excel%202016%3C%2FP%3E%3CP%3EI'm%20trying%20to%20make%20a%20formula%20that%20adds%20up%20the%20values%20in%20cells%20A1%3AD1%20of%20all%20worksheets%20with%20%22Data%22%20in%20the%20worksheet%20name.%3C%2FP%3E%3CP%3EI%20created%20a%20named%20range%20%22sheetnames%22%20with%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3D%22'%22%26amp%3BMID(GET.WORKBOOK(1)%26amp%3BT(NOW())%2CFIND(%22%5D%22%2CGET.WORKBOOK(1))%2B1%2C255)%26amp%3B%22'!%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eto%20pull%20all%20the%20worksheet%20names%2C%20add%20single%20quotes%20and%20an%20exclamation%20mark.%3C%2FP%3E%3CP%3EThen%20I%20filter%20that%20into%20a%20new%20named%20range%20%22data_sheets%22%20with%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DIF(ISNUMBER(SEARCH(%22Data%22%2Csheetnames))%2Csheetnames)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhich%20pulls%20out%20all%20the%20sheet%20names%20that%20contain%20%22Data%22%2C%20and%20replaces%20the%20rest%20with%20FALSE.%26nbsp%3B%20So%20if%20I%20have%20worksheets%20%22Test%22%2C%20%22Data%201%22%2C%20%22Data%202%22%2C%20%22Data%203%22%2C%20%22Not%204%22%2C%20then%20data_sheets%20is%20equal%20to%26nbsp%3B%7BFALSE%2C%22'Data%201'!%22%2C%22'Data%202'!%22%2C%22'Data%203'!%22%2CFALSE%7D%3C%2FP%3E%3CP%3EIf%20%22Data%201%22%20has%201%2C2%2C3%20in%20A1%2CB1%2CC1%2C%20%22Data%202%22%20has%204%2C5%2C6%2C%20and%20%22Data%203%22%20has%207%2C8%2C9%2C%20then%20the%20sum%20should%20work%20out%20to%2045.%3C%2FP%3E%3CP%3EI%20try%20the%20array%20formula%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DSUM(IFERROR(INDIRECT(TRANSPOSE(data_sheets)%26amp%3BTRANSPOSE(%7B%22A1%22%3B%22B1%22%3B%22C1%22%3B%22D1%22%7D))%2C0))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ebut%20it%20returns%200.%26nbsp%3B%20If%20I%20manually%20evaluate%20(press%20F9%20with%20it%20highlighted)%20the%20INDIRECT%20function%20first%2C%20and%20then%20evaluate%20the%20rest%20of%20the%20formula%2C%20I%20get%20the%20expected%2045.%26nbsp%3B%20But%20if%20I%20evaluate%20the%20IFERROR%20without%20first%20evaluating%20the%20INDIRECT%2C%20it%20replaces%20all%20the%20entries%20with%200%20instead%20of%20just%20the%20errors%2C%20and%20then%20the%20sum%20is%20zero.%3C%2FP%3E%3CP%3EIs%20there%20any%20way%20to%20get%20IFERROR%20to%20behave%20%22correctly%22%20with%20INDIRECT%20in%20this%20case%3F%26nbsp%3B%20Or%20is%20there%20any%20way%20to%20trim%20the%20%22FALSE%22%20values%20from%20the%20named%20range%20data_sheets%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2576512%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-2582064%22%20slang%3D%22en-US%22%3ERe%3A%20Dynamically%20look%20up%20values%20in%20sheets%20based%20on%20sheet%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2582064%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1110375%22%20target%3D%22_blank%22%3E%40dbaillie%3C%2FA%3E%26nbsp%3BPerhaps%20easier%20to%20spend%20some%20time%20reorganising%20the%20workbook%20and%20put%20%3CSTRONG%3Eall%3C%2FSTRONG%3E%20Sheets%20with%20%22Data%22%20in%20the%20name%2C%20next%20to%20each%20other.%20Add%20an%20empty%20sheet%20directly%20before%20and%20after%20this%20group%20of%20sheets%20and%20call%20them%2C%20for%20instance%2C%20%3CSTRONG%3EDfirst%3C%2FSTRONG%3E%20and%20%3CSTRONG%3EDlast%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20you%20can%20use%20a%20formula%20like%20in%20the%20picture%20below%20to%20do%20the%20SUM%20you%20described.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Screenshot%202021-07-24%20at%2007.01.15.png%22%20style%3D%22width%3A%20753px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F298120i36D74F767D49CD69%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Screenshot%202021-07-24%20at%2007.01.15.png%22%20alt%3D%22Screenshot%202021-07-24%20at%2007.01.15.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EWhen%20you%20add%20another%20%22Data%22%20sheet%2C%20just%20make%20sure%20that%20it%20is%20positioned%20between%20the%26nbsp%3B%3CSTRONG%3EDfirst%3C%2FSTRONG%3E%20and%20%3CSTRONG%3EDlast%20%3C%2FSTRONG%3Esheets.%20By%20the%20way%2C%20I%20had%20the%20numbers%201%2C2%2C3%20and%204%20in%20A1%3AD1%20on%20each%20of%20the%203%20Data%20sheets.%20Hence%20the%20SUM%20of%2030.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2582217%22%20slang%3D%22en-US%22%3ERe%3A%20Dynamically%20look%20up%20values%20in%20sheets%20based%20on%20sheet%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2582217%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1110375%22%20target%3D%22_blank%22%3E%40dbaillie%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20an%20Helper%20sheet%20(can%20be%20hidden)%20in%20the%20attached%20file%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Demo.png%22%20style%3D%22width%3A%20220px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F298129i10AE3FFA43B7D58B%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Demo.png%22%20alt%3D%22Demo.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20notes%20only%3A%3CBR%20%2F%3E-%20If%20you're%20sure%20you'll%20always%20target%20A1%3AD1%20in%20each%20'Data'%20sheet%20why%20not%20adding%20A1%3AD1%20at%20the%20end%20of%20your%20'sheetnames'%20named%20formula%3F%3CBR%20%2F%3E-%20Given%20that%20the%20file%20must%20be%20macro%20enabled%20(due%20to%20the%20legacy%20GET.WORKBOOK%20function%20-%20how%20long%20will%20MSFT%20maintain%20it...%3F%3F%3F)%20a%20few%20VBA%20lines%20of%20code%20would%20easily%20put%20your%20'Data'%20sheet%20names%20on%20an%20Helper%20sheet%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2588243%22%20slang%3D%22en-US%22%3ERe%3A%20Dynamically%20look%20up%20values%20in%20sheets%20based%20on%20sheet%20name%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2588243%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3BThanks.%26nbsp%3B%20This%20looks%20like%20a%20good%20solution%2C%20as%20long%20as%20I%20can%20trust%20my%20users%20to%20duplicate%20the%20worksheet%20between%20the%20existing%20ones.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20any%20way%20to%20reference%20all%20the%20sheets%20without%20referring%20to%20specific%20sheets%3F%26nbsp%3B%20The%20first%20and%20last%20sheets%20of%20the%20workbook%20should%20be%20consistent%2C%20so%20if%20there%20isn't%20a%20way%20to%20refer%20to%20all%20sheets%2C%20I'll%20refer%20to%20them.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20any%20way%20to%20use%20the%20sheet%20ranges%20with%20sheet-specific%20named%20ranges.%26nbsp%3B%20If%20I%20have%20Sheet1%2C%20Sheet2%2C%20Sheet3%2C%20with%20Sheet1!NamedRange%2C%20Sheet2!NamedRange%2C%20and%20Sheet3!NamedRange%2C%20respectively%2C%20is%20there%20any%20way%20to%20do%20SUM(Sheet1%3ASheet3!NamedRange)%3F%26nbsp%3B%20A%20quick%20test%20gives%20me%20a%20%23NAME%3F%20error.%26nbsp%3B%20Then%20t%20shouldn't%20be%20too%20hard%20to%20add%20some%20error%20checking%20for%20the%20sheets%20that%20don't%20have%20the%20named%20range.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Windows 10, Excel 2016

I'm trying to make a formula that adds up the values in cells A1:D1 of all worksheets with "Data" in the worksheet name.

I created a named range "sheetnames" with 

 

 

="'"&MID(GET.WORKBOOK(1)&T(NOW()),FIND("]",GET.WORKBOOK(1))+1,255)&"'!"

 

 

to pull all the worksheet names, add single quotes and an exclamation mark.

Then I filter that into a new named range "data_sheets" with

 

 

=IF(ISNUMBER(SEARCH("Data",sheetnames)),sheetnames)

 

 

Which pulls out all the sheet names that contain "Data", and replaces the rest with FALSE.  So if I have worksheets "Test", "Data 1", "Data 2", "Data 3", "Not 4", then data_sheets is equal to {FALSE,"'Data 1'!","'Data 2'!","'Data 3'!",FALSE}

If "Data 1" has 1,2,3 in A1,B1,C1, "Data 2" has 4,5,6, and "Data 3" has 7,8,9, then the sum should work out to 45.

I try the array formula 

 

 

=SUM(IFERROR(INDIRECT(TRANSPOSE(data_sheets)&TRANSPOSE({"A1";"B1";"C1";"D1"})),0))

 

 

but it returns 0.  If I manually evaluate (press F9 with it highlighted) the INDIRECT function first, and then evaluate the rest of the formula, I get the expected 45.  But if I evaluate the IFERROR without first evaluating the INDIRECT, it replaces all the entries with 0 instead of just the errors, and then the sum is zero.

Is there any way to get IFERROR to behave "correctly" with INDIRECT in this case?  Or is there any way to trim the "FALSE" values from the named range data_sheets?

6 Replies

@dbaillie Perhaps easier to spend some time reorganising the workbook and put all Sheets with "Data" in the name, next to each other. Add an empty sheet directly before and after this group of sheets and call them, for instance, Dfirst and Dlast.

 

Then you can use a formula like in the picture below to do the SUM you described.

Screenshot 2021-07-24 at 07.01.15.png

When you add another "Data" sheet, just make sure that it is positioned between the Dfirst and Dlast sheets. By the way, I had the numbers 1,2,3 and 4 in A1:D1 on each of the 3 Data sheets. Hence the SUM of 30.

@dbaillie 

 

With an Helper sheet (can be hidden) in the attached file

 

Demo.png

 

As notes only:
- If you're sure you'll always target A1:D1 in each 'Data' sheet why not adding A1:D1 at the end of your 'sheetnames' named formula?
- Given that the file must be macro enabled (due to the legacy GET.WORKBOOK function - how long will MSFT maintain it...???) a few VBA lines of code would easily put your 'Data' sheet names on an Helper sheet

 

@Riny_van_Eekelen Thanks.  This looks like a good solution, as long as I can trust my users to duplicate the worksheet between the existing ones.

 

Is there any way to reference all the sheets without referring to specific sheets?  The first and last sheets of the workbook should be consistent, so if there isn't a way to refer to all sheets, I'll refer to them.

 

Is there any way to use the sheet ranges with sheet-specific named ranges.  If I have Sheet1, Sheet2, Sheet3, with Sheet1!NamedRange, Sheet2!NamedRange, and Sheet3!NamedRange, respectively, is there any way to do SUM(Sheet1:Sheet3!NamedRange)?  A quick test gives me a #NAME? error.  Then t shouldn't be too hard to add some error checking for the sheets that don't have the named range.

@L z. Thanks for the response.  Yes, adding A1:D1 to the named formula would have simplified things.  My oversight.

I realized I would need macros enabled only after I spent a good chunk of my day trying to figure out how to make this work. Had I known that, I would have gone straight to VBA.

 

Aside, I got it working without the helper sheet like so (with your update adding A1:D1):

 

=SUM(IFERROR(SUMIF(INDIRECT(data_sheets), "<>"),0))

 

INDIRECT appears to work as an array formula only inside certain other functions (SUMIF, COUNTIF, SUM, probably others) but not in others (specifically in this case, IFERROR).  I imagine this has something to do with the underlying implementation of the different functions, but I don't have the skill or time to dig more into it.

@Riny_van_Eekelen

With your response as an inspiration, I tried to implement the following:

 

=SUM(FirstSheet:LastSheet!A1:D1*ISNUMBER(SEARCH("Data",MID(CELL("filename",FirstSheet:LastSheet!A1:D1),FIND("]",CELL("filename",FirstSheet:LastSheet!A1:D1))+1,31))))

However it appears that CELL won't work on the array of references, it gives a #VALUE! error.

Basically I figured I could sum A1:D1 on all the sheets, multiplying them by the output of ISNUMBER, which is checking whether each cell is on a sheet with "Data" in the sheet name to zero out the ones from other sheets.

 

Is there any way to make something like this work?

@dbaillie Difficult to visualise what exactly you are trying to achieve. Allowing users to add sheets to their liking and trying to capture every possible mistake they might make isn't all that easy. For instance, how do you prevent them from making spelling errors when they name the new sheet, or how do you intend to capture a sheet name like "Dta4" which should have read "Data4"? You'll probably spend 20% of your time making a good looking template and 80% to make it "fool" proof. Not my line of expertise, I'm afraid. 

 

With named ranges, scoped to single worksheets, perhaps PowerQuery can help. All it requires is a Refresh. The attached workbook contains a very simplistic PQ solution. It finds all named ranges called "data" in sheets where the name contains "Data" or "data". Perhaps something you could use.