Understanding VBScript "RC18" for decoding the formula - excel

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.

Related

Offset formula logic clarity

I am trying to get year to desired month total of personal expenditure sub categories. After researching stackoverflow, I found a formula seemingly appropriate for my requirements. I found it shifting the desired area by one row down during formula evaluation. I modified the formula by hit and trial on adhoc basis which is giving the correct results. To me the initially chosen formula appeared quite appropriate. I have shown below the sample data sheet and the evaluation steps of the original and modified formula. Could someone explain particularly the offset portion as to why it was going wrong for the initially chosen formula and how the modification helped in solving the problem. Somehow I am not able to get conceptual clarity on this issue.
Sample Data files
Personal_Accounts evaluated with formula A
Personal_Accounts evaluated with modified formula
Offset works by specifying:
A cell from you which you will offset (A1 in this example) then specifying how many rows and columns to move from that position, and then how tall and wide to make the range.
The number of rows to move down: In this case the number of rows down is determined by Match(). Match() here will return the number of rows down in the range A1:A9 that the value SS can be found. The answer is 5. Offset now is looking at Range A1 + 5 rows: A6
The number of columns to move across: Here we move 1 column. No funny business. New range is B6
The number of rows to include in the range from that start point: Here COUNTIFS() is used to return the number of times SS is found in the range A2:A9. The answer is 3. So the range will start at B6 and include three rows down in the range. Essentially B6:B8.
Finally, the number of columns to include in the range: Here it's 7 since that's what you have in cell A13, so your range is now B6:H8
OFfseT() returns that range and Sum sums it up
You subtracted one from the results of MATCH() and correctly moved that formula to produce B5:H7. You could have also changed the search range in MATCH() to A2:A9, which would probably make more sense from a readability standpoint.
Lastly, your COUNTIFS() could just be COUNTIF() since you are not evaluating multiple conditions.
So if I had to write this from scratch, I would use:
=Sum(Offset(A1, Match(A2:A9, A12, 0), 1, Countif(A2:A9, A12), A13)
Which will get you the same correct answer, without any math on Match() results.
Offset has two main functions - either to move to cell (target) using specified number of rows and columns from the starting point, or to select range of specified number of rows and columns starting in the target cell. Your original formula has issue in this part
MATCH(A12;A1:A9;0)
matched cell is fifth therefore the offset moves 5 rows down ending in A6, because it starts in A1 + 5 rows. Then it moves 1 column to be in B6 and then creates range of 3 rows in total and 7 columns = B6:H8. So you need to deduct 1 from the result of the match function to end up in the right row.
For better understanding imagine if the SS value was in the first row of the range A1:A9 (in A1) - then the offset would move from A1 one row down to A2 although you wouldnt want it to move at all.
look at your basic offset formula definition.
Offest (REFERENCE CELL, HOW MANY ROWS TO MOVE FROM REFERENCE, HOW MANY COLUMNS TO MOVE FROM EFERENCE, HOW MANY ROWS TO RETURN, HOW MANY COLUMNS TO RETURN)
so if you set your reference cell to A1 and you want to return the result in A2, you need to move down 1 row from your reference cell.
OFFSET ($A$1,1,0,1,1)
Now if we look at the match portion of your equation, MATCH return what position the information is in. So if we want to find the match position of the information in A2 in a range going from A1:A100, Match is going to tell you that the information in A2 is in the 2nd position of the column. Or more precisely it returns a value of 2.
So now we need to tell offset how far down to reach the 2nd position. We dont actually want it to move down 2 rows to get to the second position since our reference point is A1 which is the first row. As a result we really want to go down 1 row to get to the second row. So you want 1 less from your match results which you correctly did by doing Match(...)-1

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.

Repeating nested functions in 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

Resources