I have a Macro in my Excel and this Macro writes a formula to one cell in my Excel sheet.
The code of my Macro that does this is like below:
Range("F10").Select
ActiveCell.FormulaR1C1 = "=SUMIF('1'!C2,C7,'1'!C[2])"
and the Formula produced for the F10 cell is like below:
=SUMIF('1'!$B:$B,$G:$G,'1'!H:H)
Can someone help me understand this code? What does '1' , ! , [] and ... do exactly?
In general I want to know what is the output of " =SUMIF('1'!$B:$B,$G:$G,'1'!H:H)" and why the macro generates this formula?
You have chosen unfortunate sheet names.
"=SUMIF('1'!C2,C7,'1'!C[2])"
The '1' is the sheet name. Sheet names can be wrapped in single quotes, but you only need to do that if the sheet name contains a space.
The ! is a separator between sheet name and cell reference.
C2 means column 2, which is column B. You are using R1C1 syntax for the formula, so the C identifies the following number as related to a column.
C7 means the 7th column, so, column G.
'1'! again means the sheet name and the separator.
C[2] means from the current column (the C) go 2 to the right. If the formula returns column H for that, it means that the current cell must be in column F.
R1C1 referencing is not widely used in worksheets these days, but in VBA it makes relative cell referencing very easy. The brackets mean "relative to the current location" and the R or C will determine whether it's a row or column that needs to be traversed.
R1C1 is Row 1, column 1, in other words, cell A1. This is an absolute reference.
R[4]C[-2] is a relative reference. It will be evaluated based on the current cell and from that position will add 4 rows (positive value 4 for rows) and subtract 2 columns (negative value -2 for columns). So, if the current cell is H4, which translates to R4C8, a reference to R[4]C[-2] means F2.
So I'm assuming you know what
=sum($B:$B)
does? If not, then you need to take a basic Excel tutorial before aksing this quesiton.
!
The $B:$B in my formula is the range. It refers to the range that is the enitre column B on the current worksheet. If you wanted to sum up column B on a different worksheet then you specify that range like worksheetname!$B$B or if the worksheet name has spaces in it then use quotes like 'worksheet name'!$B$B.
Hence '1'!$B$B refers to column B on a worksheet named '1'
[]
So what do the square brakcets do? That is from the R1C1 notation. So normally you reference a range absolutely. So when you use a range like $B$B, you are refereing to column B no matter where you call that function from (i.e. no matter which cell your function is in). R1C1 notation is Excel's way of allowing to refer to cells relatively, or in other words refer to their locations relative to the cell your formula is written in. c2 in R1C1 notation is actually still absolute, it means the second column (i.e. B). c[2] is now relative, the [2] means two columns to the right of the cell your formula is in.
SUMIF
SUMIF is an excel formula that only sums up the portion of a range that meets a certain condition. In your case we cannot say what it will output because we don't know what is in your sheet. But basically for
=SUMIF('1'!$B:$B,$G:$G,'1'!H:H)
The '1'!$B:$B is the range of values you want to test your condition against, the $G:$G is your condition (which I think should point to a single cell rather than a range btw...) and finally '1'!H:H is the range of values that you want to sum.
So if you want to sum values in columns H only when the values in column B are greater than 10 and you sheet 1 looks like
B G H
________________
1 | 5 '>10 1
2 | 12 2
3 | 13 3
The result of =SUMIF('1'!$B:$B,$G:$G,'1'!H:H) will be 5 (i.e. adding 2 and 3 from column H because in column B 12 and 13 were larger than 10 but 5 was not and thus does not meet the criteria for being part of the sum.
Related
Need Help on Named Ranges in Formulas:
I have a second workbook ('TEST.xlsx') as the destination, referencing worksheet-scoped named ranges (in 12 columns X 75 rows) in the source workbook ('FLOW.xlsx'). I want to create a formula that will match a look-up value (a date entered into cell C3 in TEST that will return the matching named range IF there are 2 or more blank cells in that matched named range/column and the remaining named ranges/columns in that set of 12 columns with 2+ blank cells. The 12 separate columns in the source workbook ('FLOW') are named by month, year and location (ex., "jan_2019_class.1","feb_2019_class.1", etc.), the worksheet columns being C, H, M, R, W, AB, AG, AL, AQ, AV, BA, and BF. The rows are 80-155. I've only been able to make a simple working COUNTBLANK formula in my TEST workbook, ex.:
=COUNTBLANK('[FLOW.xlsx]Class_1-Chart'!jan_2019_class.1)
But NOT for successive columns (with different named ranges and the columns are non-sequential); and I can't figure out the functioning formula to combine with this to get the count AND data returned by criteria as described above. Please, no VBA/macros.
Thank you in advance for the help!
'TEST.xlsx' Screen Shot-RVSD
FLOW.xlsx- sample screenshot
There are many approaches but I personally prefer the use of helper rows/columns/cells and named ranges.
In my demonstration I used two class attendant schedule in two different year from January to June as shown below (they are sitting in Column C to M in my example):
As shown above, I have added two helper rows on top of each schedule. The first helper row is used to find out if there is 2 or more vacancies in each month, if so returns TRUE. I have given the name check.2019.class.1 and check.2021.class.5 for each of them.
The second helper row is simply showing the range name of each month such as jan_2019_class.1, feb_2019_class.2 etc. I have given the name NameRng.2019.class.1 and NameRng.2021.class.5 for each of them.
On the TEST sheet I have the following set up:
where the look up value in cell C3 is actually returned by a formula so it can be "dynamically" changed by the user. Please note in the following formula I used a name ClassNo which is essentially the value from cell B3.
=B2&"_"&B1&"_class."&ClassNo
I have also named cell C3 as Start_MthYrClass which will be used in my following formula.
The formula for looking up the first available month in 2019 if the start month is jan_2019_class.1 is:
=INDEX(NameRng.2019.class.1,MATCH(1,(TRANSPOSE(ROW($1:$11))>=MATCH(Start_MthYrClass,NameRng.2019.class.1,0))*Check.2019.class.1,0))
Please note it is an array formula so you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar otherwise they will not function correctly.
The logic is to first "filter" the range NameRng.2019.class.1 using this formula =TRANSPOSE(ROW($1:$11))>=MATCH(Start_MthYrClass,NameRng.2019.class.1,0), in which ROW($1:$11) represents {1;2;3;4;5;6;7;8;9;10;11} and TRANSPOSE will turn it into {1,2,3,4,5,6,7,8,9,10,11}. This range of numbers represents the column index in that specific range which is Column C to M (in your case it would be ROW($1:$56) as your data is in Column C to BF). Then I use MATCH to return the start column index of the look up month jan_2019_class.1, and it should return 1 as this month starts in the 1st place/column in the range NameRng.2019.class.1. So this is what I am actually comparing: {1,2,3,4,5,6,7,8,9,10,11}>=1, and it will return {TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE}.
Then I multiply the above result with range Check.2019.class.1 which is essentially {FALSE,0,FALSE,0,TRUE,0,FALSE,0,TRUE,0,TRUE}. Then I will get {0,0,0,0,1,0,0,0,1,0,1}. FYI in Excel TRUE=1 and FALSE=0, so TRUE x FALSE = 0 while TRUE x TRUE = 1.
Lastly, I use MATCH to find out the position of the first 1 in the above result which is the 5th place/column, and then use INDEX to return the corresponding value from range NameRng.2019.class.1 which is mar_2019_class.1.
Here is a more universal formula which allows you enter it in the first cell C6 and drag it down to apply across board, if you have given names to the relevant cells and ranges in the same way as what I have demonstrated.
=IFERROR(INDEX(INDIRECT("NameRng."&B6&".class."&ClassNo),MATCH(1,(TRANSPOSE(ROW($1:$11))>=MATCH(Start_MthYrClass,INDIRECT("NameRng."&B6&".class."&ClassNo),0))*INDIRECT("Check."&B6&".class."&ClassNo),0)),"")
It is also an array formula so you MUST press Ctrl+Shift+Enter upon finishing the formula in the formula bar.
It is essentially the same formula as the first one but I have added IFERROR to return a blank cell if there is no match, and I used INDIRECT to refer to the named ranges dynamically based on the year and class number chosen.
Now, if I change the look up criteria to mar_2021_class.5, here is an updated result:
Let me know if you have any questions. Cheers :)
I have two sheets containing various columns in which two columns contains document number and prices. Now I want to match cell K3 (a document number ) of Sheet 1 within column D (all document numbers) of Sheet 2. If match exists, then i want the corresponding price mentioned in Column V to be returned.
This is the formula I've tried:
=VLOOKUP(K3,'Sheet1 (2)'!D2:D896,22,FALSE)
The VLOOKUP documentation is helpful here, especially points 2 and 3 which correspond to parts 2 and 3 of the formula.
The range where the lookup value is located. Remember that the lookup value should always be in the first column in the range for VLOOKUP to work correctly. For example, if your lookup value is in cell C2 then your range should start with C.
The column number in the range that contains the return value. For example, if you specify B2: D11 as the range, you should count B as the first column, C as the second, and so on.
So change the second D to V in your lookup range, and change the column to 19 instead of 22 - D is the 1st column, E the second and so on till V, the 19th column of the lookup range.
=VLOOKUP(K3,'Sheet1 (2)'!D2:V896,19,FALSE)
if VLOOKUP is not your thing, consider INDEX/MATCH.
=INDEX('Sheet1 (2)'!V:V,MATCH(K3,'Sheet1 (2)'!D:D,0))
=VLOOKUP(K3,Sheet1!D3:E8,2,FALSE)
In formula second last argument is the column number of values.
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.
I have a column of data in say A1:A10 in worksheet A. In worksheet B I have a range for data from A1:J1. I need the data from the column in worksheet A to be transposed into the data range in worksheet B. I can Paste Special and paste in the values but I need the information in worksheet A to update automatically that in worksheet B.
Any help or advice is appreciated.
Please select A1:J1 in worksheet B and enter:
=TRANSPOSE('worksheet A'!A1:A10)
with Ctrl+Shift+Enter.
Copy this:
=INDEX(A!$A$1:$A$10, COLUMN())
into your cells A1 through J1 in worksheet B (so that all cells contain the exact same formula).
Basically, the INDEX function grabs a specified element out of your vertical array A1:A10 in worksheet A (the worksheet name is specified before the exclamation point). Which element to choose is given by the second parameter. Here, we simply use the COLUMN() function, which returns 1 for column A, 2 for column B and so forth...
EDIT:
If you want to use a different data column as the source of your data, simply replace the A!$A$1:$A$10 with anything else you like, e.g. Sheet3!$C$10:$C$23.
To place the copied data somewhere differently, copy the formula above into all the target cells, wherever they may be. BUT: If the target cells do not start in column A, you need to subtract an offset from the number returned by COLUMN(), so that you pick element 1 in your first target cell. So, for example if you want to place the data into cells J5 through S5, you will need to copy this formula into each cell:
=INDEX(A!$A$1:$A$10, COLUMN() - 9)
This is because COLUMN() returns 10 for column J, but you want that cell to contain element 1 of your source data column, so we subtract 9 to get from 10 to 1. In column K, COLUMN() will return 11, subtracting 9 yields 2, so INDEX(...) will then return the second data element for that cell, and so on.
I can't seem to wrap my head around an answer for this. I can find duplicates when using a formula that looks at a single sell and compare it to a range of cells, and copy that formula down every row; afterward I can just sum the results. However, I want a single formula in a single cell.
I want to take a range of cells in one column, compare that entire set to a range of cells in another column, and sum all duplicate cells where the cell in column 1 matches a cell in column 2. For this exercise, all cells in each column are unique within that column, they are only potentially duplicate between columns.
For example:
C1 C2
R1 1 2
R2 2 6
R3 3 7
R4 4 1
R5 5 8
I want a single column sum with a result of 2 (the duplicate cells in C1 is 1 and 2).
This will ultimately be converted into a VBA script however I can work with starting with a single formula. If starting out at VBA is easier then that works for me too.
My specific question is: which functions in excel do I use to accomplish this task?
Thanks for your help.
I believe the formula you want in cell C3 is:
=IF(ISERROR(VLOOKUP(A1,$B$1:$B$5,1,FALSE)),0,1)
This will return a 1 if your column-1 value appears in column 2, and a 0 if it doesn't. Then you can simply copy it down for the extent of your data (changing the referenced range as needed) sum the results of column 3. (Note that the FALSE is important as it forces exact matches; otherwise, Excel will display approximate matches, which will compromise the accuracy of your results.)