Forum Discussion
Copying a formula to adjust the cell range automatically.
See the attached workbook for a solution, and for more information, including hyperlinks to documentation and explanatory articles. Read the _Info worksheet, and the information below.
Using Excel's LET function in a formula allows you to do multiple calculations within one formula, in a way that is easier to understand and typically more efficient than the alternatives. Below is the formula in cell C12 of the Master worksheet. Note that I have included soft line breaks (Alt + Enter) to break it into multiple lines, making it easier to read. I used 8 "variables" to store intermediate calculations; the last line (which does not define a variable name) determines what is stored as the cell's value.
=LET( month_num, MONTH(C$1), category, $B12,
col_offset, (month_num - 1) * 4, max_rows, 151,
RBCV, SUMIF( OFFSET('RBC Visa'!$C$4, ,col_offset, max_rows), category, OFFSET('RBC Visa'!$B$4, ,col_offset, max_rows) ),
RBCM, SUMIF( OFFSET('RBC MC'!$C$4, ,col_offset, max_rows), category, OFFSET('RBC MC'!$B$4, ,col_offset, max_rows) ),
CIBC, SUMIF( OFFSET('CIBC Visa'!$C$4, ,col_offset, max_rows), category, OFFSET('CIBC Visa'!$B$4, ,col_offset, max_rows) ),
debit, SUMIF( OFFSET(Debit!$C$4, ,col_offset, max_rows), category, OFFSET(Debit!$B$4, ,col_offset, max_rows) ),
RBCV + RBCM + CIBC + debit)
Excel will modify the cell references in formulas that you copy from cell to cell, unless you use absolute references in the formula, in which both the column letter(s) and row number are preceded by a dollar sign ($), such as $AH$21. (Without any dollar signs, the reference is a relative reference.)
In your case, you do want the cell references to be modified, but only in particular ways. That involves mixed references. So when referencing the first row of the current formula (as I did when calculating the month number month_num), a dollar sign precedes the 1 only: MONTH(C$1). Regardless of where such a formula is copied, that reference will refer to the first row. Similarly, the retrieval of category name from (always) column B uses a reference of $B12.
As for the OFFSET function, it returns a rectangular range of one or more cells relative the the first argument. Review the documentation. By default, the number of rows offset is zero, so I left that parameter out. Similarly, the default number for height (in rows) and width (in columns) is 1, so I omitted the latter parameter.
If you believe that you will have more than 151 transactions per month on any of the expense detail worksheets, change that number (max_rows) in the formula before you start copying it.