Fetch the appropriate formula from another sheet based on reference - excel

Instead of writing the formula multiple times at multiple instances, I'd like excel to detect the formula based on a defined reference.
Column F should be calculated based on the Operation defined in column E. In this example, I can use IF() as I only have 4 Operations but this question is linked to another scenario where I have over 20 Operations.
I have all the formula's ready but I'd love to understand if there is way to use the appropriate formula based on "Operation" Column.

I would approach your situation by labeling your Column A, B, C, or X, Y, Z or something similar for consistent readability with your formulas.
Second thing I would do is build your formula table similar to what you have except that I would change your formula from being sheet and cell references to being you variable references:
A+B+C+D
A-B-C-D
A/B/C/D
A*B*C*D
Make sure the formulas are written with operators the same way you would if you were using a cell reference.
Then based on your lay out on sheet 3 I would use a VLOOKUP:
=VLOOKUP(E2,SHEET4!$A$2:$B$5,2,0)
Place that in F2 and copy down. That will tell you if you are pulling the right formula or not.
In G2 use the following:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(F2,"A",A2),"B",B2),"C",C2),"D",D2)
Check this to see that your substitution of your values for your variables is correct.
Then select cell H2 to make it the active cell. Then go Formula Ribbon and select Define Name from the Definied Names section.
Give a name for your formula such as ANSWER.
For your formula use
=evaluate(G2)
once you have added the defined formula to the list of formulas go to cell H2 and enter the following:
=ANSWER
now if you want to avoid all those helper columns, you can substitute one formula into the other.

Related

How to filter and add together one column using the information of another column, which I need to find using a cell

Basically, I want to filter, and then add together, one column('Essay Sentences'!H6:H1400) using the information of another column, which I need to find using a cell(A2).
For example, I have the column 0011c ‘Essay Sentences’!H6:H1400 and I want to add together only the 0011c’s that also have at least one 0012a. But I only get the “0012a” column from searching for it with cell A2.
A2 could be anything from the header 'Essay Sentences'!F4:AZ4, so I need the formula to look for any column under these headers.
Also, I need to use the FILTER() and SUM() functions.
Here's what I got so far:
=SUM(FILTER('Essay Sentences’!H6:H1400, INDEX('Essay Sentences'!F4:AZ1400, 0, MATCH(A2, 'Essay Sentences'!F4:AZ4, 0)) > 0))
Link to Source Data1
Link to Source Data2
A mock up example as below:
Please see below solution using SUMPRODUCT based on your mock-up example.
The following formula is in Cell C10 in my solution. You cannot drag it across board as it has been hard-coded to specific named ranges. Use INDIRECT for more flexibility if needed.
=SUMPRODUCT(FRUITS*(SNACKS>0)) or =SUMPRODUCT(INDIRECT($A10)*(INDIRECT(B$9)>0))
The three formulas are essentially the same depends on if you prefer to use Named Ranges (eg. FRUIT stands for cell B2:B5) or actual ranges (eg. B2:B5) in the formula.
Using INDIRECT enables you to drag the formula across board but the trade off is that it will slow down the calculation. If you are only comparing one column with another, just use the Name or the actual range in the formula.

EXCEL: When dragging cells to the right I need to use information stored in columns not rows (Not sure how to ask this properly)

It's probably a simple problem, but I did not even know the keywords to google it ;/. Let's say I have this data :
Now I also have this litle formula:
If I know drag the C cell to the right, Excel will attempt the following caluclation:
=2+B1
What I want him to do is to attempt this calculation
=2+A2
Of course the easiest solution would be to store my initial data in one row instead of 1 column, but it is really inconvenient for me. Thanks for any help
You can use the indirect() method to reference a cell by it's "String identifier", i.e. "A3". When filling out to the right, use CONCATENATE() and COLUMN() to create your String identifiers {A1,A2,A3,A4,A5...} as required:
=2+INDIRECT(CONCATENATE("A";COLUMN()-2))
This will result in the following:
Side-Node: If you want this for some x/y-Grid-Generation, you can also be lazy,
and just insert =COLUMN() for every cell from "A1 - Z1" and ROW() for every cell from "A2 - A24".
(Or even avoid these at all and directly perform your actual calculation by using column() and row() as replacement for your x/y.
You may try using a combination of the INDIRECT and COLUMN functions:
=2+INDIRECT("A"&(COLUMN()-2))
You would paste the above formula into cell C1, and then drag across to the right however many columns/rows you wanted to cover.
This would result in the following:
This works because COLUMN()-2 returns 1 for the C column, 2 for the D column, and so on. Therefore, the formula will be calling INDIRECT on A1, A2, etc. for column C, D, and so on.
In general, if you want relative references to move down as cells are dragged to the right, you can use this:
Instead of:
= 2+A1
Do:
= 2+INDEX($A:$A,COLUMN()+<offset>)
Where <offset> is whatever offset you need. The offset will change depending on which column the starting formula is located in.
INDEX should be preferred over INDIRECT because INDIRECT is volatile (must recalculate after any change to the workbook) but INDEX is not (only recalculated when one of the inputs the formula, in this case $A:$A, changes).

Excel 2010 showing items in drop down list where criteria is met

I'm going around in circles with this and have tried as many different options as I can think of - VLOOKUP, INDEX/MATCH, IF etc... but I'm failing everytime.
I need to create a drop down list in sheet 3 (column A) that gets populated with values in column B of sheet 2, only where the value in column G of sheet 2 is "Yes". The values in sheet 2 look as follows:
This is what is shown at present:
I am using the following formula within Name Manager to get to this point:
=OFFSET(Units!$B$11,0,0,COUNTIF(Units!$B$11:$B$202,">"""),1)
and although this works, it's not quite as I need it to be. I've tried using Index/Match, small, VLOOKUP etc... but Excel reports that either I've too few arguments or can't reference a worksheet.
I've literally been though as many excel websites as I can find but no one seems to cover creating drop down lists where the drop down is dependent on a specific selection.
The values within sheet 2, column B are obtained from a hidden sheet and collected using the following query:
=IF(ISERROR(INDEX(All_Units!$D$2:$D$660542,MATCH(Units!A11,All_Units!$C$2:$C$660540,0))),"",INDEX(All_Units!$D$2:$D$660542,MATCH(Units!A11,All_Units!$C$2:$C$660540,0)))
I wondered whether I could take this query and use it to generate the drop down list, something along the lines of:
=INDEX(Units!$B$11:$B$202,MATCH(Units!$G$11:$G$202="Yes",Units!$G$11:$G$202,0)))
but this returns an error. The closest I got was using the OFFSET formula above but performing a COUNTIF, however; this unsurprisingly wouldn't tie Column B to the corresponding 'Yes' in column G.
Does anyone have any ideas as to how I can get this working?
You will need to create another sheet into which you will place an array formula. This formula will return all the desired output.
so create a sheet and name it something like DataVal
In A2 put the following array formula:
=IFERROR(INDEX(Units!$B$11:$B$202,MATCH(1,(Units!$G$11:$G$202="Yes")*(COUNTIF($A$1:A1,Units!$B$11:$B$202)=0),0)),"")
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter. If done correctly then Excel will put {} around the formula.
Then copy/drag down far enough to ensure capturing every possible return.
Then use another INDEX/MATCH to create the named range (OFFSET() is volatile and should be avoided when possible). The formula would be something like:
=DataVal!$A$2:INDEX(DataVal!$A:$A,MATCH("ZZZ",DataVal$A:$A))
This will dynamically set the named range to the extent of the dynamic list created by the array formula above.

Create a custom function in Excel

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.

Excel DSum function multiple criteria

Hello guys, I have been trying to implement the DSUM function but failed to figure it out. I looked through the previous DSUM posts on here and still don't understand. This is my problem:
on I8:L9, i have to implement the DSUM to calculate the "calculated quantity sold" for each item, ie Textbook, Novel,notepad and laptop. from A9-A16
I am supposed to use these values on B20 - B23. I don't know how to make sure i have multiple criteria for the DSUM function.
I tried this and it worked only for the first function: =DSUM(bookstore,J8,I8:I9)
This gave me the correct value only for Textbook, It summed up the quantities for textbook. What I want to do now is replicate it down so that it is the same for all the other elements.
Please help. thank you
With the setup you have then using this formula in B20 copied down will give the cumulative total of the categories
=DSUM(Bookstore,J$8,I$8:I9)
so that will give the sum for Textbook only in B20 but then in B21 it will be the sum for Textbook and Novel combined....then in B22 Textbook, Novel and Notepad (A22 should be "Notepad" to match I11)
To get the sum for the item in question only you could subtract the previous values above, i.e. use this version in B20 copied down
=DSUM(Bookstore,J$8,I$8:I9)-SUM(B$19:B19)
....but all things being equal I agree with user667489, except SUMIF is usually preferable for a single condition, i.e. in B20 copied down
=SUMIF(A$9:A$16,A20,D$9:D$16)
Is there any particular reason that you need to use DSUM to do this? You're making life very hard for yourself. You could do this much more easily via SUMPRODUCT or by using a pivot table.
When you create a column of conditions on the same variable, and you specify that as your constraint range for DSUM, excel sums over your database range where any of those conditions are true. You can't make it apply the constraint from just one row, unless that's the only row other than the column headings. So you need to put your different constraints for the same variable in different columns. This gets very messy.
Here is a sumproduct formula that will accomplish what you're trying to do, which can be copied down:
=SUMPRODUCT(($D$9:$D$16)*($A$9:$A$16=I9))
Paste that into cells J9:J12 (or B20:B23).
Another alternative having entered the DSUM formula in B20 is to select the range B20:B23 and choose Data > What If Analysis > Data Table... Column Input Cell: I9, OK. This should give the formulas below:
B20: =DSUM(bookstore,J8,I8:I9)
B21:B23: {=TABLE(,I9)}
Note: the Table formula cannot be entered from the formula bar, it is automatically generated by the Data Table command. (The same procedure could also be used to enter formulas in J9 and J10:13.)
I'd also recommend using SUMIF, or looking at PivotTables which could be used easily for this and create the list of categories for you.

Resources