Excel - How to use the value of a cell as the row value of another cell? - excel

Here is my problem:
I have a cell (V4) containing the value 444. I want to use this value in the formula of another cell (M12) in the following way. I want the formula to be equivalent to =MIN(L12:L444) but instead of 444 I want to refer to cell V4 which contains the value 444. But when I type in =MIN(L12:L(V4)) it obviously doesnt work so how do I do it? Sorry if I didn't explain it very well. :S

Would this work for you:
=MIN(L12:INDIRECT("L"&$V$4))
From: Excel - INDIRECT and Using the value in a cell as a cell reference in a formula?

INDIRECT will work and is closest to the solution you described, but I prefer OFFSET, which uses proper references. (For example, if you insert a column in the sheet before L, INDIRECT will break while OFFSET will just update its reference as expected.
Two ways to go with OFFSET:
1 - Start at L$1 and go down $V$4-1 rows. (This will work with $V$4 as you've defined it now.)
=MIN(L12:OFFSET(L$1,$V$4-1,0))
2 - In $V$4, provide the height of the range you want.
=MIN(OFFSET(L12,0,0,$V$4,0))
It's hard to make suggestions without more context, but I'm sure you can tweak one of these patterns to meet your needs.

I am not sure if you are trying to include all of the values in 1 column and then on non-contiguous cell. If so, it should look like = Min(L12:L444,V4) . The L12:L444 looks at the value in every cell in the L column from 12 - 444.
So you can check individual cell (A3, D15, Q54) with commas, or a range of cells (A3:Z54) with a colon. Or a range and a specific cell like above =Min(L12:L444, V4).

Related

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).

How to Use Cell Text From Cell Being Checked by COUNTIF in Excel

What I'm wanting to do is have a formula in one cell that counts the values in a range that conform to a lookup of that range cell's value compared to another cell.
OMG, now that I look at it, that is totally confusing. Let me try to clarify a lot here.
Say we have Cell1, which will hold the counting formula. I have a list of values in a two-column table, Table1. The range, Range1 that Cell1 will be counting from is a range of cells that have List Validation in them. Table1 holds references to all values that can result from those Lists, in column 1. I have another cell, Cell2, which holds a number value. Column 2 of Table1 holds values that reference Cell2. I need to count the number of values from Range1 whose row matches in Table12 match the value in Cell2. Is there a way I can do this with COUNTIF without referencing each cell individually? Is there some shorthand (like Range.currentValue) that I can use to get the value of the cell currently being checked? The range is 11 rows long, and I need to do a second range that has 12 rows counted.
Man, I really don't know how to clarify that any more... I'll post this for now, in case anyone can understand what I'm saying and knows the answer, while I work on a sample spreadsheet I can upload.
I did my best to visually represent what I'm trying to accomplish:
http://gyazo.com/b83295baf3b156683a5c39b40c806504
Extended explanation: http://gyazo.com/4048802050e3dcfca7aee238acc2f7dd
Use a helper column, say, between the brown and the first blue or at the right of the setup. Use a vlookup like
=vlookup(brownvalue,BluetableRange,2,false)
Then do a countif on the helper column
=countif(HelperColumn,"<="&GreenCellAddress)
You can hide the column with the helper if it upsets your spreadsheet design.
You can (and probably should) use a helper column as Teylyn suggests. But, for when that may be inconvenient, you can also use an array formula:
=SUM(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch))
To enter it as an array formula, type "ctrl-shift-enter" after editing the formula, rather than just "enter"
Rough explanation: since rangeoflists is in a place where a single value is expected, the countifs is calculated once for each value, and the array of results is passed to sum. Use the "evaluate formula" feature to see the intermediate result array.
Afterthought: It occurs to me now that this does rely on listlookupcolumn containing unique values. (Almost certainly true in this example.) You can modify the formula a bit to get around this:
=SUM(SIGN(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch)))
The SIGN function will keep you from double counting.
Again, you must use "ctrl-shift-enter" for this to work. (Yes, as I'm sure others are ready to point out, you can also use the sumproduct hack in this instance.)

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.

IF function - is there a way to avoid repeating formula

Can't believe I don't know this, but is there a way to avoid repeating a formula in an if statement if the logical test is dependent on it?
i.e.
=IF((SUMIFS formula)=0,"",SUMIFs formula)
I want to replace that SUMIFS function in the false scenario with something short that will tell it to just programmatically repeat the formula it originally tested for. Repeating the formula twice has to have detrimental effects on processing speed. Negligible, maybe, but want to go for best-practices here. Thanks.
You can force an error like #DIV/0! and then use IFERROR, e.g.
=IFERROR(1/(1/SUMIFS_formula),"")
You can assign a Name to a formula and use the Name..............See:
Assigning a name to a formula
Relevant excerpt -
For example, let's suppose we frequently use a formula like:
=SUM(A1:A100)-SUM(B1:B100) and this resides in A101 and is copied across many columns on row 101. It would be better in this case to
create a custom formula that does this in each cell on row 101. Here
is how;
1) Select cell A101 (this is vital).
2) Go to Insert>Name>Define and
in the "Names in workbook" box type: SalesLessCosts
3) Now click in
the "Refers to" box and type: =SUM(A1:A100)-SUM(B1:B100) then click
Add.
Now you can replace the formula in cell A101 with: =SalesLessCosts.
You can also copy this across row 101 and it will change its relative
references just as the formula =SUM(A1:A100)-SUM(B1:B100) would. The
reason it does this is all down to the fact we selected A101 before
going to Insert>Name>Define and used relative references in
=SUM(A1:A100)-SUM(B1:B100) when we added it to the "Refers to" box.
If all you need to do is hide zeroes, there is an easy way:
Select all cells where you wish to hide zeroes
Go into Custom Number Formatting
Set format to "General;General;"
The custom formatting has a structure of [positive numbers];[negative numbers];[zeroes]
By making the last part blank you are effectively hiding zeroes, but showing everything else.
The advantage over conditional formatting is that you can use this on any background.
A neat trick which I sometimes use is to hide the cell value completely by using a custom format of ";;;". This way you can put images inside the cells, like the conditional formatting ones, and not see the value at all.
Try using the SUBSTITUTE function like this :
=SUBSTITUTE( VLOOKUP( H4; $D$5:$E$8; 2; 0 ); $H$1; $I$1 )
Here is an example:
Here the formula I don't want to repeat twice is the VLOOKUP function.
The result of VLOOKUP is a string found in another table (ex : "Green").
I want to check if that string matches a specific string value in $H$1 (here, "Yellow").
If it does, SUBSTITUTE replaces it with$I$1 (the error string you want. Here, "FORBIDDEN").
If it doesn't, it displays the VLOOKUP result string (the normal authorized output, like "Green").
This is useful for me because my actual formula is quite long, so I don't want to write it twice.
I also dont want to use two different cells, because I'm already applying this formula on 10 columns, meaning I should add an extra 10 columns to make it work.
In some scenarios, MAX() or MIN() can do a wonderful job.
E.g., something like this:
=IF(SUMIFSformula>0,SUMIFSformula, 0)
Can be shortened to this:
=MAX(0,SUMIFSformula)
The LET formula can be used for this exact scenario. You can define the formula as a variable and then within that same cell you can reference the variable in your formula.
The LET formula format looks like this:
=LET(name,name_value,calculation)
SUMIFS Example
Here's how it would work with your SUMIF example so that you don't have to repeat the formula:
In this screenshot we have an array A1:B7. We want to sum the values (Col B) if the name in ColA is "apple".
For this we have a standard SUMIFS formula of
=SUMIFS(B1:B7,A1:A7,"apple")
The formula is showing in E2. The result is shown in E3.
To put this into the IF statement without having to repeat the formula we can use LET as shown in the screenshot.
We create a variable with the SUMIFS formula as the value of that variable. We then write our IF statement using the variable name instead of rewriting the formula multiple times.
=LET(name,name_value,calculation)
Variable name: sumapples
Variable value: SUMIFS(B1:B7,A1:A7,"apple")
Calculation: IF(sumapples=0,"",sumapples)
Put together in the LET function it looks like this:
=LET(sumapples,SUMIFS(B1:B7,A1:B7,"apple"),IF(sumapples=0,"",sumapples))
This LET function can be used in any Excel formula, and is very useful for shortening long formulas that have repetition.
Optional: Extra complexity
If you want to you can get extra complicated by naming multiple variables.
=LET(name,name_value,name2,name_value2,calculation)
Since Excel 2007, the IFERROR statement does what the OP asked. From the help file:
Description:
Returns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula. [italics mine]
Syntax:
IFERROR(value, value_if_error)
I've since realised that this was already answered by #barry houdini above.
Here is a hack - depending on whether you are just interested in the displayed value, or whether you need to use the value in another formula:
Put your SUMIF formula in the cell (without the IF part)
Create a conditional formatting rule which sets the font color to the background color when the cell value is 0
And hey presto, you get the desired result.
As I said - it's a hack, but it does prevent the double evaluation.
There is no "clean" solution that I am aware of.

Excel Reference To Current Cell

How do I obtain a reference to the current cell?
For example, if I want to display the width of column A, I could use the following:
=CELL("width", A2)
However, I want the formula to be something like this:
=CELL("width", THIS_CELL)
Several years too late:
Just for completeness I want to give yet another answer:
First, go to Excel-Options -> Formulas and enable R1C1 references. Then use
=CELL("width", RC)
RC always refers the current Row, current Column, i.e. "this cell".
Rick Teachey's solution is basically a tweak to make the same possible in A1 reference style (see also GSerg's comment to Joey's answer and note his comment to Patrick McDonald's answer).
Cheers
:-)
Create a named formula called THIS_CELL
In the current worksheet, select cell A1 (this is important!)
Open Name Manager (Ctl+F3)
Click New...
Enter "THIS_CELL" (or just "THIS", which is my preference) into Name:
Enter the following formula into Refers to:
=!A1
NOTE: Be sure cell A1 is selected. This formula is relative to the ActiveCell.
Under Scope: select Workbook.
Click OK and close the Name Manager
Use the formula in the worksheet exactly as you wanted
=CELL("width",THIS_CELL)
EDIT: Better solution than using INDIRECT()
It's worth noting that the solution I've given should be preferred over any solution using the INDIRECT() function for two reasons:
It is nonvolatile, while INDIRECT() is a volatile Excel function, and as a result will dramatically slow down workbook calculation when it is used a lot.
It is much simpler, and does not require converting an address (in the form of ROW() COLUMN()) to a range reference to an address and back to a range reference again.
EDIT: Also see this question for more information on workbook-scoped, sheet dependent named ranges.
EDIT: Also see #imix's answer below for a variation on this idea (using RC style references). In that case, you could use =!RC for the THIS_CELL named range formula, or just use RC directly.
You could use
=CELL("width", INDIRECT(ADDRESS(ROW(), COLUMN())))
=ADDRESS(ROW(),COLUMN(),4) will give us the relative address of the current cell.
=INDIRECT(ADDRESS(ROW(),COLUMN()-1,4)) will give us the contents of the cell left of the current cell
=INDIRECT(ADDRESS(ROW()-1,COLUMN(),4)) will give us the contents of the cell above the current cell (great for calculating running totals)
Using CELL() function returns information about the last cell that was changed. So, if we enter a new row or column the CELL() reference will be affected and will not be the current cell's any longer.
A2 is already a relative reference and will change when you move the cell or copy the formula.
=ADDRESS(ROW(),COLUMN())
=ADDRESS(ROW(),COLUMN(),1)
=ADDRESS(ROW(),COLUMN(),2)
=ADDRESS(ROW(),COLUMN(),3)
=ADDRESS(ROW(),COLUMN(),4)
Without INDIRECT(): =CELL("width", OFFSET($A$1,ROW()-1,COLUMN()-1) )
I found the best way to handle this (for me) is to use the following:
Dim MyString as String
MyString = Application.ThisCell.Address
Range(MyString).Select
Hope this helps.
Inside tables you can use [#] which (unfortunately) Excel automatically expands to Table1[#] but it does work. (I'm using Excel 2010)
For example when having two columns [Change] and [Balance], putting this in the [Balance] column:
=OFFSET([#], -1, 0) + [Change]
Note of course that this depends on the order of the rows (just like most any other solution), so it's a bit fragile.
There is a better way that is safer and will not slow down your application. How Excel is set up, a cell can have either a value or a formula; the formula can not refer to its own cell. You end up with an infinite loop, since the new value would cause another calculation... . Use a helper column to calculate the value based on what you put in the other cell. For Example:
Column A is a True or False, Column B contains a monetary value, Column C contains the folowing formula:
=B1
Now, to calculate that column B will be highlighted yellow in a conditional format only if Column A is True and Column B is greater than Zero...
=AND(A1=True,C1>0)
You can then choose to hide column C
Full credit to the top answer by #rick-teachey, but you can extend that approach to work with Conditional Formatting. So that this answer is complete, I will duplicate Rick's answer in summary form and then extend it:
Select cell A1 in any worksheet.
Create a Named Range called THIS and set the Refers to: to =!A1.
Attempting to use THIS in Conditional Formatting formulas will result in the error:
You may not use references to other workbooks for Conditional Formatting criteria
If you want THIS to work in Conditional Formatting formulas:
Create another Named Range called THIS_CF and set the Refers to: to =THIS.
You can now use THIS_CF to refer to the current cell in Conditional Formatting formulas.
You can also use this approach to create other relative Named Ranges, such as THIS_COLUMN, THIS_ROW, ROW_ABOVE, COLUMN_LEFT, etc.
EDIT: the following is wrong, because Cell("width") returns the width of the last modified cell.
Cell("width") returns the width of the current cell, so you don't need a reference to the current cell. If you need one, though, cell("address") returns the address of the current cell, so if you need a reference to the current cell, use indirect(cell("address")). See the documentation: http://www.techonthenet.com/excel/formulas/cell.php
Reference to a cell that include this formula (self reference):
address(row();column())
E.g. getting the value of the cell above:
indirect(address(row()-1;column()))
Or what the OP asked:
=Cell(width;address(row();column()))

Resources