Repeating nested functions in Excel - excel

I am trying to use one single formula across multiple cells for calculating a value, dependent on the specific row and column of which the formula is applied, but relative to the row itself. Sounds a little cryptic, so an example:
I have the following three rows,
Row 91: 10|Wheat|60,00|0,00
Row 92: 11|Banana|91,20|1,00
Row 93: 12|Milk|200,00|182,00
Where the | represents a separation between each column, which are named by letter (as Excel does by default). So the value of cell A92 would be 11. Now, column D is generated using the following formula: =((C92/100)*D11), basically taking the value of cell C92 (91,20) dividing it by 100, and then multiplying it by whatever value is held in cell D11 (the same column as the one I am generating a value in, but using the value from column A for reference of what value to multiply by).
Now, seeing as I need to use this formula in many rows, I want to basically to something like this: =VALUE(=CONCATENATE("D"; ROW();)/100)*CELL("address")), but this is not a valid formula. To put it in words:
Get the value of cell D+current row number (for example D9 at row 9 etc.)
Divide this by 100
Multiply this by the value of current column letter+current row number (E69 at cell E69 etc.)
I hope someone has an idea of how to achieve this.

You can do this with OFFSET, but you may very well get circular references or errors if your data is not clean.
=(C92/100)*OFFSET(D92,$A92-ROW(),0,1,1)
The OFFSET says: Get the value of a 1 row 1 column range that is (the value in A92 - current row number) rows offset from this row, but in the current column), which for your data is D11

I think this should do your job:
Sub Test()
Cells(ActiveCell.Row, "D").FormulaR1C1 = "=RC" & ActiveCell.Column & "*RC3/100"
End Sub

Related

Understanding VBScript "RC18" for decoding the formula

i am making an application for which i have to read the formulas from excel. After many search i was able to read the formula behind a field. But i was not able to understand it. Can somebody help me in this regard?
Formula is
ActiveCell.Offset(0, 5).FormulaR1C1 = "=RC18-3*(AVERAGE(C19)/1.128)"
Actually i am not uderstanding what is RC18 and C19 at clicking the specific fields it gives this formula.
=$R27-3*(AVERAGE($S:$S)/1.128)
how RC18 = $R27
That's Relative Cell Reference. R/C do not mean a cell address.
RC18 means "The cell in the same row, column 18"
In relative reference R means "row" and without a number next to it, means the same row as the cell wherein the formula resides. (an R without a C would mean the entire row, e.g., R1 would be Row 1.)
C18 means column 18. Similarly, without the C19 means Column 19, or, column S.
Using square brackets for the number indicates an offset, where negative would be a left offset (columns) or an up offset (rows), and otherwise offsets are right (columns) and down (rows), so:
R[1]C[1]
Would be the cell one row below and one column to the right of the active cell, and likewise:
R[2]C[-3]
Would be the cell two rows below and three columns left of active cell.
That's because the formula is built by using R1C1 notation as explained here and here(external)
Having RC18 would mean that it refers to the 18th column from the cell you are and, when you check on a given cell in that column, the formula is "translated" to cell in 27th row for that range (the column 18). If any, please show a screen capture to keep on checking.

Adding all the values below the current cell in Excel

I am trying to display the total sum of all the numbers for a particular column. I want the sum to be displayed above the column as follows:
21 30
A B
6 5
6 10
6 10
3 5
I know I can sum the values and display it at the bottom of the column using =SUM(A3:INDIRECT("D"&ROW()-2)), however I am not getting a way to display it at the top of the column.
Please guide.
Based on the comments and the previous answers I suggest following formula, entered in cell A1:
=SUM(OFFSET(A$2,0,0,ROWS(b:b)-1))
You can then copy/paste to the right till second last column.
You could also modify your formula in A1 like this to achieve the same:
=SUM(INDIRECT("A2:A"&ROWS(A:A)-2))
But then you cannot copy/paste to the right...
A more general approach with your idea would be:
=SUM(INDIRECT(ADDRESS(ROW()+1,COLUMN())&":"&ADDRESS(ROWS(A:A),COLUMN())))
You can then copy/paste to the right till last column.
Some explanations:
Both formula sums up every value in the range from A2 till the bottom of column A (i.e. for Excel 2010 this would be A2:A1048576)
It doesn't matter if there are blanks or cells without value; the formula sums up only the numbers
I put A$2 and B:B in the OFFSET formula to avoid circular references, since I'm writing in cell A1 and I cannot write A$1 nor A:A
With the INDIRECT formula you don't have to worry about circular references
Further commenting (sorry, I don't have the credits to comment at the right place under the question):
Phylogenesis formula =SUM(A3:A65535) could also do the work, isn't it?
Didn't understand your question at first, because you talk of "sum of all the numbers for a particular row" but then you sum columns, isn't it?
When I'm doing something like this, I prefer to not include any empty cells beneath the range I'm summing, because I've had errors in the past as the result of including them (usually because there's a cell way down in the column somewhere that I'm not expecting to have a value). I'm assuming that A & B are your column headers. Assuming that, here is how I would do it. This is your formula for cell A1:
=SUM(OFFSET(A$1,2,0,COUNTA(A$3:A$65535)))
Explanation
I'm updating this with a brief explanation, per the OP's request.
According to ExcelFunctions.net:
The Excel Offset function returns range of cells that is a specified number of rows and columns from an initial supplied range.
The function reference for OFFSET is:
=OFFSET(reference, rows, cols, [height], [width])
What this formula does is create a dynamic range based on the number of cells in the selection, relative to cell A$1. This is an offset of two rows and no columns, which starts the range at A$3. The height of the range is the total number of filled cells in the range A$3:A$65535. The assumption here is that there are no blank cells in the range, which there were not in the sample data.

Find the first empty cell in the same column/row

I am trying to work out a formula that will give me the row number of the first empty cell in a column. Currently I am using:
=MATCH(TRUE, INDEX(ISBLANK(A:A), 0, 0), 0)
This works fine, unless the formula is put in the same column as the column I am searching in, in which case it does some sort of circular reference or something. Is there a formula I can use instead which will work when placed in the same column as it searches in?
Another way to do it
=MIN(IF(A2:A6="",ROW(A2:A6)))
you have to press CTRL+SHIFT+ENTER
The difference is that this will give you the Row number of the first empty cell
The previous answer will give the position (how many rows from the starting row) of the first empty cell... Both ways are valid depending on your needs
All you have to do is count the non blank cells in a column : COUNTA
e.g.
=COUNTA(E:E)
If you want to find the first blank cell from further down the column, then set the column range e.g. E23:E100 and add the cell number -1 to it.
=COUNTA(e23:E1000)+23-1
=MATCH(TRUE,INDEX(ISBLANK(INDIRECT("R[1]C:R1048576C",0)),0,0),0)+ROW()
This formula above returns the row number of the first blank cell in the current column and below the current row, but it does not need to be entered in row 1 of the worksheet.It replaces the A:A target range with a range that starts in the next row and continues to the end of the sheet and then adds the current row number to the result to add the skipped rows back in. This avoids the circular reference since it starts on the next row.
Using the INDIRECT(ref_text, [a1]) function with the second argument set to FALSE or 0 allows you to pass ref_text in R1C1 notation (eg. B5 as R5C2).
The R1C1 notation also has a lesser known syntax for describing a location as an offset from the current position. If the number following the R or C is enclosed in square braces then it is treated as a +/- offset from the current cell or zero offset if omitted (eg. "R[-1]C[-1]" in cell B5 returns A4 and "RC" in B5 returns itself B5).
The R1C1 location of R[1]C:R1048576C from the formula defines a range starting at the current cell with a RC offset of 1,0 and ending at fixed row 1048576 with a 0 column offset from the current cell. When placed in cell C3 it will be equivalent to C4:C1048576 and in A1 is equivalent to A2:A1048576. If you needed to dynamically set the max row number, you use INDIRECT("R[1]C:R"&ROWS(A:Z)&"C",0) but since current version Excel has a row limit of 1,048,576 it makes sense to just set it.

Excel: Find intersection of a row and a column

My question is how can I find an intersecting cell of a specific column and row number?
My situation is this: with some calculations I find two cells, lets say B6 and E1. I know that I need a row of the first one and a column of the second one. So I could just use ROW and COLUMN functions to get the numbers. After that, I need to find an intersecting cell. Which would be E6 in this example.
I would just use INDEX(A1:Z100;ROW;COLUMN) but I don't know the exact area that I'm going to need - it depends on other stuff. I could use something like A1:XFG65000 but that is way too lame. I could also use a combination of INDIRECT(ADDRESS()) but I'm pulling data from a closed workbook so INDIRECT will not work.
If this would help to know what is this all for - here's a concrete example:
I need to find limits of a section of a sheet that I would work with. I know that it starts from the column B and goes all the way down to the last non-empty cell in this column. This range ends with a last column that has any value in first row. So to define it - I need to find the intersection of this last column and the last row with values in B column.
I use this array formula to find the last column:
INDEX(1:1;MAX((1:1<>"")*(COLUMN(1:1))))
And this array formula to find the last row:
INDEX(B:B;MAX((B:B<>"")*(ROW(B:B)))
Last column results in E1 and last row results in B6. Now I need to define my range as B1:E6, how can I get E6 out of this all to put into the resulting formula? I've been thinking for a while now and not being and Excel expert - I couldn't come up with anything. So any help would really be appreciated. Thanks!
You can use an Index/Match combination and use the Match to find the relevant cell. Use one Match() for the row and one Match() for the column.
The index/match function to find the last cell in a sheet where
column B is the leftmost table column
row 1 is the topmost table row
data in column B and in row 1 can be a mix of text and numbers
there can be empty cells in column B and row 1
the last populated cell in column B marks the last row of the table
the last populated cell in row 1 marks the last column of the table
With these premises, the following will return correct results, used in a Sum() with A1 as the starting cell and Index to return the lower right cell of the range:
=SUM(A1:INDEX(1:1048576,MAX(IFERROR(MATCH(99^99,B:B,1),0),IFERROR(MATCH("zzzz",B:B,1),0)),MAX(IFERROR(MATCH(99^99,1:1,1),0),IFERROR(MATCH("zzzz",1:1,1),0))))
Since you seem to be on a system with the semicolon as the list delimiter, here is the formula with semicolons:
=SUM(A1:INDEX(1:1048576;MAX(IFERROR(MATCH(99^99;B:B;1);0);IFERROR(MATCH("zzzz";B:B;1);0));MAX(IFERROR(MATCH(99^99;1:1;1);0);IFERROR(MATCH("zzzz";1:1;1);0))))
Offset would seem to be the way to go
=OFFSET($A$1,ROW(CELL1)-1,COLUMN(CELL2)-1)
(The -1 is needed because we already have 1 column and 1 row in A1)
in your example, =OFFSET($A$1,ROW(B6)-1,COLUMN(E1)-1) would give the value in E6
There is also ADDRESSS if you want the location: =ADDRESS(ROW(B6),COLUMN(E1)) gives the answer $E$6
The following webpage has a much easier solution, and it seems to work.
https://trumpexcel.com/intersect-operator-in-excel/
For example, in a cell, type simply: =C:C 6:6. Be sure to include one space between the column designation and the row designation. The result in your cell will be the value of cell C6. Of course, you can use more limited ranges, such as =C2:C13 B5:D5 (as shown on the webpage).
As I was searching for the answer to the same basic question, it astounded me that there is no INTERSECT worksheet function in Excel. There is an INTERSECT feature in VBA (I think), but not a worksheet function.
Anyway, the simple spacing method shown above seems to work, at least in straightforward cases.

Return a cell reference for use in a formula

I have an excel workbook with paired data sets of the type [columnA=time:columnB=data]
There are thousands of data points.
What I'm looking to do is this: I have a table of time values on a separate sheet. I want to find the corresponding cell in the data column of the data worksheet (column B) and return an average of the values of 5 cells before it and 40 cells after it.
By searching the questions I came up with this formula to return a cell address of the exact value I'm looking for, however I can't put it into an average formula.
=CELL("address",INDEX('EMG Data'!B1:B10000,MATCH(C5,'EMG Data'!A1:A10000,0),1))
C5 is the time value I know I want to get the data value pair for.
use a combination of average, offset and match instead, offset will allow you to change the number of rows/columns in the range:
=IFERROR(AVERAGE(OFFSET($B$1,MAX(MATCH(C5,A:A,0)-6,0),0,46)),"SOMETHING IS WRONG")
MATCH
MATCH() will find the right value in column 1 and return its position in A:A.
OFFSET
To OFFSET to the corresponding row compared to cell B1 you should do MATCH()-1 (otherwise a Match in cell A1 yielding a 1 will OFFSET B1 by 1 row thus showing the value of B2!!
You want to offset B1 by 0 columns so put that as third parameter.
Moreover you want to have the 5 rows before as well so this becomes MATCH()-6, to avoid trying to retrieve data from rows with negative row numbers we use MAX(MATCH()-6,0).
As you want the 5 cells before and the 40 cells after you want the height to be 5 + 1 + 40 = 46 cells completing the OFFSET parameters with that number for the height parameter
AVERAGE
Final step is to get the actual average, as the OFFSET() function gives back a range of cells this is seen as a normal range input for the AVERAGE function.
IFERROR
In case the value you look for is not found in column A:A than MATCH() will give an error which will work its way through the nested functions, Most of us would capture such event with IFERROR() and transform the error into something user friendly, or have an alternative function/result in that case

Resources