Is it possible to external reference a path once? - excel

So I've got two workbooks, where Workbook A pulls out different cells from different worksheets of Workbook B and adds them altogether. Problem is, it does this so much that it can end up causing a formula in excess of the 8000+ characters because of the filepath.
Is there a formula or method that would help shorten this? Perhaps a way where you only refer to the file path once and then every cell reference inside the formula knows it's to that same file path? The process of constantly repeating the filepath just to get the Worksheet & Cell Reference is what causes the current simple addition formula to balloon. I cannot use a SUM function it would capture other data not intended.

If you have a criteria for what values you would want to sum, you can use the SUMIF/S function to add these values considering your criteria.
Ex.: =SUMIF('D:\test\[test_file.xlsx]Sheet1'!A1:A200,">="&3,'D:\test\[test_file.xlsx]Sheet1'!A1:A200)
will add values equal to or more than 3 in range A1:A200 of Sheet1 in test_file.xlsx in D:\test\ directory.
Another method is typing in the source workbook directory in a cell and making use of an INDIRECT function to do your addition.
Ex.: If you enter 'D:\test\[test_file.xlsx]Sheet1'! as text in cell B1, you can add values of A2, A5 and A7 by typing in =sum(indirect(B1&"A2"),indirect(B1&"A5"),indirect(B1&"A7")) thus significantly reducing character length.
You can as well try to reference all relevant cells of the source workbook to another sheet in the destination workbook and make your addition from that "helper" sheet.
I'd prefer the SUMIF/S option and having a reliable criteria to work with.

What seems to have worked out for me is using SUMPRODUCT. It has cut down on the formula length considerably. As there is a standard criteria of which I wanted to multiply across different worksheets, Array one refers looks for matching criteria from the first worksheet and then uses Array 2 for each worksheet.

Related

How to prevent the CONCATENATE function from producing double quotes around character string? [duplicate]

I want to refer to a cell value in another closed workbook with a formula (not VBA!). The Sheet name is stored as a variable (in the following example, C13 is "Sheet2").
If the other file is open, then following works:
=INDIRECT("[myExcelFile.xlsm]" & C13 & "!$A$1")
If the file is closed, the above formula doesn't work, as there is no absolute path given. But I got it work with following (give attention to ' instead of ":
='C:\data\[myExcelFile.xlsm]Sheet2'!$A$1
Now I want to replace the hardcoded "Sheet2" with a dynamic referenced value, means with C13 (as seen in the first code snippet).
Does anybody know a solution without using VBA or other libraries?
There is definitively no way to do this with standard formulas. However, a crazy sort of answer can be found here. It still avoids VBA, and it will allow you to get your result dynamically.
First, make the formula that will generate your formula, but don't add the = at the beginning!
Let us pretend that you have created this formula in cell B2 of Sheet1, and you would like the formula to be evaluated in column c.
Now, go to the Formulas tab, and choose "Define Name". Give it the name myResult (or whatever you choose), and under Refers To, write =evaluate(Sheet1!$B2) (note the $)
Finally, go to C2, and write =myResult. Drag down, and... voila!
I too was looking for the answer to referencing cells in a closed workbook. Here is the link to the solution (correct formula) below. I have tried it on my current project (referencing a single cell and an array of cells) and it works well with no errors. I hope it helps you.
https://www.extendoffice.com/documents/excel/4226-excel-reference-unopened-file.html
In the formula, E:\Excel file\ is the full file path of the unopened workbook, test.xlsx is the name of the workbook, Sheet2 is the sheet name which contains the cell value you need to reference from, and A:A,2,1 means the cell A2 will be referenced in the closed workbook. You can change them based on your needs.
If you want to manually select a worksheet to reference, please use this formula
=INDEX('E:\Excel file\[test.xlsx]sheetname'!A:A,2,1)
After applying this formula, you will get a Select Sheet dialog box, please select a worksheet and then click the OK button. Then the certain cell value of this worksheet will be referenced immediately.
The problem is that a link to a closed file works with index( but not with index(indirect(
It seems to me that it is a programming issue of the index function.
I solved it with a if clause row
C2=sheetname
if(c2=Sheet1,index(sheet1....),if(C2="Sheet2",index(sheet2....
I did it over five sheets, it's a long formula, but does what I need.
Check INDEX Function:
=INDEX('C:\path\[file.xlsm]Sheet1'!A10:B20;1;1)
Thank you for the great question. I want to clarify and second the solution posted by Philipp. The solution does the following:
Updates data from a closed Excel file, and
Does so "dynamically" (though not exactly - you'll see)
In his answer above, Philipp said: "The problem is that a link to a closed file works with index( but not with index(indirect("
I believe this is still true. (I'm using Office 365 here in March of 2021. It would be so nice if Microsoft allowed INDIRECT and INDEX to play nicely together with closed files!)
I can confirm that Philipp's workaround indeed works, practically speaking. From the user's point of view, it feels like what Chris, the OP, wants to do.
Let's say you want the contents of a given cell to be drawn from Sheet1, Sheet2 or Sheet3, which are from a closed file myExcelFile.xlsm. You will choose which sheet by changing the contents of a particular cell (C13, as in the OP Chris's question). One way to do this is to make it a drop-down list (using Data > Data Validation > Allow [List]). The list will have the following names:
Sheet1
Sheet2
Sheet3
(This will generalize without the drop-down list. For example, you can just type "Sheet2" into C13, and the relevant data from the closed file will appear; if you then type "Sheet3" then the data from that sheet will appear instead. Or you can have a column with the sheet names, and the next column over [with the formula below, adjusted] for the output from the closed file.)
The cell that will contain the referenced data from the other, closed sheet (myExcelFile.xlsm) will have:
=IFS(
C13="Sheet1",'C:\data\[myExcelFile.xlsm]Sheet1'!$A$1,
C13="Sheet2",'C:\data\[myExcelFile.xlsm]Sheet2'!$A$1,
C13="Sheet3",'C:\data\[myExcelFile.xlsm]Sheet3'!$A$1
)
And that's it.
It'll throw up a #REF error the first time you do this with myExcelFile.xlsm closed, but you can go to Data > Queries & Connections > Edit Links > Update Values and your values from the closed file should appear. (Personally, I set up a macro with a button to do the updating, instead of hunting through the menus.) The magic is that when you do Update Values, the data from all of the sheets in the closed file get loaded into the current Excel file - so you don't have to update every time you switch the reference to a different sheet (Sheet2 to Sheet3, for example).
Andrew
In Excel 2016 at least, you can use INDIRECT with a full path reference; the entire reference (including sheet name) needs to be enclosed by ' characters.
So this should work for you:
= INDIRECT("'C:\data\[myExcelFile.xlsm]" & C13 & "'!$A$1")
Note the closing ' in the last string (ie '!$A$1 surrounded by "")
=INDIRECT("'C:\Data["&A8&"]SheetNAME'!$G9")
where A8 contains myExcelFile.xlsm
and G9 contains your source workbook precious data.
OK,
Here's a dinosaur method for you on Office 2010.
Write the full address you want using concatenate (the "&" method of combining text).
Do this for all the addresses you need. It should look like:
="="&"'\FULL NETWORK ADDRESS including [Spreadsheet Name]"&W3&"'!$w4"
The W3 is a dynamic reference to what sheet I am using, the W4 is the cell I want to get from the sheet.
Once you have this, start up a macro recording session. Copy the cell and paste it into another. I pasted it into a merged cell and it gave me the classic "Same size" error. But one thing it did was paste the resulting text from my concatenate (including that extra "=").
Copy over however many you did this for. Then, go into each pasted cell, select he text and just hit enter. It updates it to an active direct reference.
Once you have finished, put the cursor somewhere nice and stop the macro. Assign it to a button and you are done.
It is a bit of a PITA to do this the first time, but once you have done it, you have just made the square peg fit that daamned round hole.
This seems to work with closed file: add a pivot table (rows, tabular layout, no subtotals, no grand totals) of the source to the current workbook, then reference all you want from that pivot table, INDIRECT, LOOKUPs,...
If you know the number of sheet you want to reference you can use below function to find out the name. Than you can use it in INDIRECT funcion.
Public Function GETSHEETNAME(address As String, Optional SheetNumber As Integer = 1) As String
Set WS = GetObject(address).Worksheets
GETSHEETNAME = WS(SheetNumber).Name
End Function
This solution doesn't require referenced workbook to be open - Excel gonna open it by itself (but it's gonna be hidden).
I was disappointed so I made the following workaround:
I created my INDIRECT commands and stored them just outside the desired range. Of course I get errors when the source workbook is closed but they light up when it's open.
So I open the source workbook temporarily.
Then I created a macro that copies the INDIRECT formulas from outside the range and pastes them inside the range. All cells are filled in with the right values.
The macro then copy-pastes the values of the range to the range on top of themselves. This deletes the formulas but keeps their values.
Then I close the source workbook. My values stay intact where I want them and my INDIRECT formulas stay just outside the desired range in case I want to refresh them by repeating the procedure!
This workaround saves me the error of the indirect formulas when the source workbook is closed, and most importantly it saves a lot of time in recalculations!
Hope this works for you too!

Why use INDIRECT in place of direct reference in excel

Why would one use INDIRECT(cell) instead of a direct reference to cell?
Eg, I see a sheet where there are many references
A B C
1 SHEET1 B1 =INDIRECT("'"&A1&"'!"&B1)
2 SHEET1 B2 =INDIRECT("'"&A2&"'!"&B2)
3 SHEET1 B3 =INDIRECT("'"&A3&"'!"&B3)
Why not just
A B C
1 =SHEET1$B1
2 =SHEET1$B2
3 =SHEET1$B3
Indirect vs Direct Cell Reference
does not generally auto update vs does update
example adding or removing columns
arithmetic to change row or column vs what you type is what you get
example indirect("A"&3+5) vs =A3+5 is totally different
If you want to organize your formula references and change them all on the fly it is easier with indirect (although even easier just using the naming feature) but the real reason you "need" indirect is how else are you going to change the reference in your formula without manually typing it (answer: indirect)?
use case - programmatically list and loop a range of worksheets:
sheet references using formulas and values useful for addressing cells at scale.
Build references to many different cells, worksheets or workbooks that follow a logic
The most common use of the INDIRECT function is probably when someone wants to reference to many cells which cell references follow a logical rule. For example, assume you have an Excel workbook with hundreds of sheets, one for each day and the sheet names are the dates. Now you would like to summarize some of the values in those sheets on an overview sheet. In this case, you can type in your starting date, drag it down to your final date (Excel will increment the dates). Using the INDIRECT function, you can now easily build up the references within seconds.
However, consider that INDIRECT is a volatile function which will slow down your workbook. Further, if you insert rows/columns, the INDIRECT function won’t adapt. It get’s even worse when you reference to external sheets. Since INDIRECT updates it’s value with every change in the workbook, you will get #REF errors as soon as you close the referenced sheets.
I personally avoid INDIRECT for such cases (either using VBA or by choosing a different design for my workbooks, so no INDIRECT function is necessary)
Lock a cell reference
If you have a cell reference like let’s say =A10, Excel will always adapt the reference when you insert new rows or columns (if you for example insert a row above row 10, the reference changes to =A11). You can use the INDIRECT function in order to always keep the absolute cell reference: =INDIRECT(“A10”).
With named ranges
INDIRECT can be handy with named references. Have a look at the example where you have three named ranges:
NorthAmerica: B2:B5
Europe: C2:C5
Asia: D2:D5
You can now combine the INDIRECT function with many other Excel functions like SUM, MIN, MAX and so on. In the example, the drop down selection in G1 is referenced using INDIRECT to perform the calculation for the selected range.
Dynamic dropdowns
A similar example where you can use the INDIRECT function are dynamic drop downs. In this example there are two named ranges:
Fruits: A2:A4
Vegetables: B2:B4
In cell D3, there is a dropdown where you can select “Fruits” or “Vegetables”. In E3, we have a dynamic drop down with the source =INDIRECT($D$3). If you choose “Fruits” in D3, you will have a list with the fruits in the drop down.
So, there are definitely some things where INDIRECT might be an easy solution. But as I said, it is a volatile function that locks the cell reference. In most cases you can find different, better solutions. The main reason people use it is probably the lack of knowledge of better alternatives. In addition, I assume that the average Excel user is not aware of possible problems you might run into when using INDIRECT.
Indirect is very useful with Tables. For example, I create a table tblFindings with 10 rows. Then I assign the list to =Indirect("tblFindings"). Now I add 5 rows to the table, the dropdown list automatically updates.

Reference a range of cells and keep the order when adding new rows

I am referencing a range of cells in a first sheet, to build a second sheet. Often I add rows in the middle of the first sheet. In the second sheet that is referencing the first, there is a skip in the cell number where I have added a row.
SHEET 1: Contains my main list, that is updated
A new row is added (A3) to SHEET 1:
SHEET 2: references Sheet 1 and pulls through the rows
However, you can see that where row 3 should contain the added row 'Rachael', it instead has shifted down to Sheet1!A4 and missed A3 out all together.
How can I fix this?
Try using this formula in sheet2:
(add it to Sheet2, A2, then copy it around.)
=offset(sheet1!$A$1,row(a2)-1,column(a2)-1,1,1)
Try to avoid formula volatilty, which means a formula recalculates on a change to the sheet even if its precedents have not changed.
Having numerous volatile formulas in a worksheet can cause performance issues.
Any formulas that utilize the OFFSET() function or the INDIRECT() function automatically become volatile. But of these two functions, INDIRECT is much worse than OFFSET. Both are volatile, but OFFSET is extremely fast, while INDIRECT is extremely slow.
DO NOT USE INDIRECT().
The best alternative is without question the INDEX() function. It is even faster than the OFFSET function and INDEX is not volatile.
So use the following formula in cell A2 of the 2nd sheet:
=INDEX(Sheet1!$1:$1048576,ROW(),COLUMN())
...and then copy as needed.
To directly answer your question - you can achieve this with the INDIRECT function. INDIRECT allows you to dynamically reference a cell through a formula, which doesn't necessarily follow Excel's "tracking" rules. Keep in mind that normally, Excel gives each cell a 'unique id', and when you initially reference any cell, the internal logic points to that specific 'unique id', and the visible reference points to the 'A1' style reference to that cell. This is done so you can insert rows and columns without unintentionally losing all of your references.
It is generally not a good idea to do what I'm about to show, because you lose the inherent benefit that direct references provide (in general: easier to maintain). However, to show you how it would work, see below [this assumes you want one header row, and that the column on your results sheet should match the column on your raw data sheet]:
=INDIRECT("Sheet1!R"&ROW()+1&"C"&COLUMN())

How to reference cell within '' worksheet title

I have the names of the tabs/worksheets (M-61,M-62,M-63W) at the top row (A1, B1, C1...etc)
I am trying to get a sum of several cells within the different sheets:
=SUM('M-60'!H21,'M-60'!H43,'M-60'!H86,'M-60'!H87,'M-60'!H97,'M-60'!H98)
However, right now I’m referring to the sheet itself, and have to apply the same formula to all the other sheets. This will require me to manually go and change all the sheet titles accordingly.
I was wondering if there is any way to reference the top row with the sheet titles within the formula so it automatically refers to the row text instead of me having to manually change the sheet title.
Edit
Now i got the reference to work, just wondering how would I do a sum of several cells in that tab
=INDIRECT("'"&$F1&"'!H87",TRUE)
Maybe:
=SUM(INDIRECT("'"&C1&"'!H21"),INDIRECT("'"&C1&"'!H43"),INDIRECT("'"&C1&"'!H86:H87"),INDIRECT("'"&C1&"'!H97:H98"))
(though there may well be a much smarter way).
You can use the INDIRECT function, which uses a string as an argument and converts it to a range. So
=M-60'!H21
is the same as
=INDIRECT("M-60'!H21")
or, if Sheet name is stored in, say, cell C1:
=INDIRECT(C1&"'!H21")
Your example has a SUM, though, which requires some adaptations. This your example:
=SUM('M-60'!H21,'M-60'!H43,'M-60'!H86,'M-60'!H87,'M-60'!H97,'M-60'!H98)
Since you are not using a range, you can convert that SUM into simple addition. Assuming Sheet name in cell C1
=INDIRECT("'"&C1&"'!H21")+INDIRECT("'"&C1&"'!H43")+INDIRECT("'"&C1&"'!H86")+INDIRECT("'"&C1&"'!H87")+INDIRECT("'"&C1&"'!H97")+INDIRECT("'"&C1&"'!H98")
This should solve your problem. More info here
By the way, if you were using a range, the OFFSET function with INDIRECT as an argument would work. But that's not necessary here.

Getting formula of another cell in target cell

How does one cell obtain the formula of another cell as text without using VBA? I can see this question has already been asked many times and the answer is always to write a custom function in VBA.
However, I found a post made in 2006 which claimed to have found the non-VBA solution but the link provided in that post is already broken.
=FormulaText(Reference) will do the trick Documentation
There is nice way of doing this without VBA. It uses XL4 macros (these are macros, but it is not VBA, as asked).
With reference to the figure 1, cells A2:A4 contain usual formulas.
Going to Formulas -> Define Name, I defined two named ranges (see fig. 2), with the information shown in cells A6:B8.
Enter in cell B2 =FormulaAsText. This will retrieve the formula in cell A2 as text.
Explanation:
The named range FormulaAsText uses =GET.CELL(info_type,reference). In this case, ìnfo_type = 6 retrieves the formula, and reference = OFFSET(INDIRECT("RC",FALSE),0,-1) uses the cell with 0 rows and -1 columns offset from the one the formula is used in.
Copy B2 and paste into B3:B4. This will show formulas in A3:A4. Cell A4 shows that the worksheet function CELL only retrieves values, not formulas (as opposed to GET.CELL).
Since FormulaAsText gets the formula from a cell at fixed offset (0,-1) from the current, I defined another range FormulaAsText2, which uses an offset (rows,cols) read from the worksheet itself. Cells D2:D4 contain =FormulaAsText2. Thus, cell D2 shows the contents of cell B3 (=OffSET(D2,1,-2)), which is FormulaAsText. cells D3:D4 show the contents of themselves. This adds some flexibility. YMMV.
PS1: The essence was taken from
http://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
PS2: Tim Williams mentioned in a comment "the old XLM GET.FORMULA()". This answer is possibly related (not the same, since this one uses GET.CELL()).
PS3: A simple VBA solution is given, e.g., in
http://dmcritchie.mvps.org/excel/formula.htm
EDIT: Complementing this nice answer, the worksheet function FormulaText is available for Excel 2013 and later.
This suggestion may be helpful for those who after retrieving a block of formulas and transporting them to a new spreadsheet want to put them to work again. Excels FORMULATEXT function is great for picking up formulas but it leaves them as unusable text strings. If you want to get them back as fully functioning formulas you have to edit each one individually to remove the string character, but here is a shortcut for larger blocks.
Get to the position where you have the required formulas as text (in other words after using FORMULATEXT - you have done a copy and (value only) paste). The next step involves highlighting all the cells you want to convert and then navigating to the [Text-To-Columns] menu option ({Data} bar on Excel 2016). You can select 'Delimited' but on the next screen just make sure you de-select any marks that do appear in your formulas. Then 'Finish'. Excel should automatically analyse the cells as containing formulas and you should now have them working again.
There is a way to do this. In my example I had a table that showed a date. The date comes from Sheet!G91. In my table I also had a column that showed the sheet name. I added two more columns to my table. The first column had column(Sheet!g91), which returns the number 7, because G is the seventh letter in the alphabet. I then converted the number to a letter (G) using another table in my workbook. In the second column that I added, I made a formula row(Sheet!G91), which returns the number 91. Note: Row and Column may appear as volatile formulas, which recalculate with every calculation of the workbook.
I wanted another column to show the formula contents of the date cell mentioned at the beginning of this post. I included the following string function (you can also use CONCATENATE).
"=" & AJ9 & "!" & AM9 & AN9
The items separated by ampersands get strung together (that is, concatenated). AJ9 in my example contains the sheet name, AM9 contains the column letter, and AN9 contains the row number.
I now have a column that dynamically updates its contents to reflect the sheet name and cell reference. The results in my workbook cell are
=Sheet!G91.
You can't. This is most likely a design choice to eliminate an average Excel user from accidentally getting something they did not want.
What you are reading is correct - writing a UDF is the solution you want.

Resources