I'm using the INDIRECT function to fetch data from another table sheet.
The direct way would be "sheetname!A5". I need it dynamically, so the sheetname is also part of the table, using indirect "indirect("'"&G$1&"'!A5"), whereas G1 contains the sheetname works fine.
INDIRECT is horribly slow, which outperform my project because I need a lot of fetches.
Is there a less powerful / more performant function to fetch data from variable sheets?
I've run into similar situations - depending on how frequently your 'results' worksheet changes, it may be best to create a formula dynamically that initially 'writes' hardcoded formuls. For example: ="='"&G$1&"'!A5".
Then copy that formula as you wish; it will create text strings that represent what you want the formulas to look like. Then copy & paste as values - you will now have the desired formula entered into each cell. By default, Excel will still display each cell as a text string, even though the format type would be general (assuming it was beforehand). From there, different solutions are possible - if you have few enough cells it may be enough to just start at the top pressing F2 then Enter, repeatedly, to get Excel to calculate each cell.
Related
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.
Is there a way to condense Excel Formula references into one reference while keeping the formula contained to one cell?
For reference the formula I am using is
=VALUE(SUBSTITUTE(RIGHT(G4,LEN(G4)-
MIN(FIND({0,1,2,3,4,5,6,7,8,9},G4&"0123456789"))+1),"-","."))
Which References G4 multiple times, I would like someway to make it so I or another user only has to change G4 in one place should the change be required.
Perhaps something like this (Not real excel formula:)
=MULTIPLEX(VALUE(SUBSTITUTE(RIGHT(#1,LEN(#1)-
MIN(FIND({0,1,2,3,4,5,6,7,8,9},#1&"0123456789"))+1),"-",".")),#1,G4)
So that the #1 does not need to be changed and the G4 being changed once updates the whole formula.
I was trying to avoid VBA so that it is a user friendly formula, I will not be the only one using it.
Side note an implementation of my theoretical multiplex formula would allow for multiple references to be made. Syntax would be like MULTIPLEX([formula],[ref1],[cell1],[ref2],[cell2],...)
Side Side note the Formula is meant to extract the number from an item identifier for sorting purposes. ABC1234-1 becomes 1234.1 so we can sort by number ascending.
Assuming the numeric part is at most 9 characters (including the "-") then you can use this formula to extract, which only references G4 once
=LOOKUP(10^10,SUBSTITUTE(RIGHT(G4,{1,2,3,4,5,6,7,8,9}),"-",".")+0)
Is it possible to output the result of a formula on a different cell without the use of VBA and without directly reffering to the cell containing the formula (e.g. writting =C1 into the output cell)?
I know that this can be easily done with the help of VBA, but I was wondering if there really is no simpler way to achieve this.
I don't think so, if you dont want VBA then all I can think of is to lock the cells so they can't be updated.
How to lock just a range of cells
As far as I know all formulas display their results within the cell they occupy.
if you want to pull a value from a cell without directly referencing the cell address, you could indirectly by using the offset function. However even the offset function needs to reference at least one cell on the worksheet.
I wrote this simple formula in an Excel sheet.
=CONCATENATE(A$1,"='",A49,"', ",B$1,"='",B49,"', ",
C$1,"='",C49,"',",D$1,"='",D49,"'")
It combines the cell value of the current row with the column headers.
I put it on E49 and if I copy-paste it anywhere in column E it adjusts nicely. I want to improve it and make it more generic and I'd rather not use VBA to keep it simple (i.e. install on any spreadsheet with simple copy paste of cell formula.)
FEATURE DONE:
Automatically adjust to change the label in column headers.
FEATURE MISSING:
Adjust to add/remove columns by editing a few characters (e.g. having a "A:D" or "A1:D1" argument in the formula. )
Automatically adjust to add/remove columns.
QUESTION:
Can I do that with a formula only? If not, is there any formula that could get close to the same result?
it depends on how complex you want the formula to be. I think its a less elegant solution, but you could theoretically build a huge IF(..) statement to test if column names exist.
IF they are not blank, perform the concat.
personally, I would use VBA, and toss it into an Add-in; that way it would open within any instance of excel on the local computer.
This seems like such an obvious thing that excel must have this feature, I just can't find it.
How can I create a custom function without using VBA? (VBA is too big of a hammer and causes security warnings, etc).
For example, I have a spreadsheet with several very complex formulas. Each of these formulas are replicated in several columns. Each column has hundreds of entries, so each one is replicated hundreds of times. If I tweak something then I have to manually fill-down or copy my change from one column to another.
A simple one looks like this:
=(Payment1 - F$12)*12 + ($D21-H21)
But what I'd like to do is:
=MyFunction(f$12,$D21,H21)
And have the actual formula for "MyFunction" written just once someplace.
I've found a few things that come close to giving me what I want. For example, in tables Excel will automatically replicate changes in a formula down the rest of the column saving you the step of manually selecting the range and doing a "Fill Down".
It will also allow relative references off of named cells, which seems equivalent of a user-defined parameter-less functions.
if you can use text to create the formula, then you can define a name to evaluate the function.
In cell A2, create a name EvalAbove, and in Refers To, enter =evaluate(A1)
This way, you can construct a formula
e.g. B1 contains SUM, B2 contains =("="&B1&"(A2:A5)")
and in B3, you can then put =EvalAbove
This means that if you change the formula name in B1, then B2 will change to show the changed formula, and B3 will change to show the result.
Note that this still counts as a macro enabled workbook, but there's no VBA code, just named ranges
You can do this for the example you show if I interpret it correctly.
If not you may be able to rearrange things slightly to conform
your function has three parameters:
The first comes from row 12 of the current column
The second from column D of the current row
The third comes from the column two to the right of the current row
I assume Payment1 is a named variable already?
Set the cursor in say F21 and then define this name
MyFunction =(Payment1 - F$12)*12 + ($D21-H21)
This will set the parameters to come from the places shown
To understand this better switch to RC mode and type the formula as:
=(Payment1 - R12C)*12 + (RC4-RC[+2])
You can now propagate down the formula through the F coloumn
=MyFunction
and it will always use the values in the corresponding F12 column Dxx and column Hxx
If you drag the formula to the next column it will use G12, Dxx and Ixx
If you want to change the formula edit it in the define name space
This is a general exception to the rule that you cannot have non-vba UDFs in Excel. Often in Excel the things you want as 'arguments' to the function are actually in fixed places (rows or columns) that can be addressed relatively.
For example you often want to perform a udf on the cell to the left
So a udf giving the cuberoot of the cell to the left would be a named formula like this:
Cuberoot =(RC[-1])^(1/3)
Or in a1 form set the cursor in B1 and type =(A1)^(1/3)
And Excel will convert it internally to the RC form
For three args - use three columns
It works and does not suffer the volatility issue mentioned about evaluate()
Yes I know this is an old posting but it may help someone with the same issue.
Bob J.