Excel - Reference within a reference - excel

I may going up the wrong tree with this.
I have the following formula, that will replace the * with the range of sheets in the workbook, and SUM the values in cell B2 on each of the sheets:
=(SUM('*'!B2)/J2)/100
This works fine, however the formula doesn't retain the '*', it replaces it with the actual range, eg:
=(SUM('Period 1:Period 4'!B2)/J2)/100
So, is it possible to either:
Retain the '*' so that the formula doesn't update, and therefore remains dynamic should I add a new tab.
Add a reference into the reference. The first sheet will always be 'Period 1', however the end sheet will change from month to month, and will be 'Period x', where x is the value in cell J2. Is it possible to reference J2 within =(SUM('Period 1:Period 5'!B2)/J2)/100? =(SUM('Period 1:Period [J2]'!B2)/J2)/100 or something similar?

The only way I know to do this is to create a 'sheet sandwich' with a 'Start' and 'End' sheet then the range is =SUM('Start:End'!B2). New sheets obviously have to be inserted between these to be included.
Note:not my solution, credit belongs to SUM across multiple sheets with variable sheet name question.

Related

Excel Double Reference in a Formula

I have an Excel Sheet called "master", one called "all-names" and one called "table-20-01".
In the master sheet I want to write a formula, that gets a sheetname out of a cell in the "all-names" sheet and that name is for example "table-20-01". I want to use that name to reference a cell in that sheet. According to my research I'm supposed to use the INDIRECT Function for that (for example Increment Worksheet References
So I thought that I could do something like this:
=INDIRECT("'" & "'all-names'!A1" & "'!$A$1")
'all-names'!A1 ==> "table-20-01"
'table-20-01'!$A$1 ==> The value I'm looking for
I use this because I want to create formulas that automatically increment sheet references. And since the sheets are not numbered as simply as sheet1, sheet2, sheet3, but table-20-01, table-20-02, ..., table-20-12, table-21-01, I have the names stored in the sheet "all-names".
Unfortunately I am getting a REF error. Anyone?

Simple VB ststment deconstruction

Could somebody please explain this simple vba statment to me? I just want to know what each part is referring to, and basically what this statement is accomplishing within my workbook. Thank you
ActiveCell.FormulaR1C1 = "='Bren Template'!R[-3]C[-6]"
This is a cell reference. In the ActiveCell (the one chosen), it will put the formula ='Bren Template'!R[-3]C[-6]. The formula breakdown is "Bren Template" is a reference to a sheet with that name. The R[-3] refers to three rows ABOVE the active cell. The c[-6] refers to three columns to the LEFT of the active cell.
So, if the active cell is H5 that formula will read ='Bren Template'!B2
If your active cell is I6, then the formula will read ='Bren Template'!C3
Note: The r[-3] and c[-6] will be "translated" from R1C1 style (i.e. Row 1 Column 1) when the formula is actually set in the cell.

How to find the maximum value in entire Excel workbook

I have an Excel workbook with many sheets (today, about thirty, and growing every day). Each worksheet is named for the date of the data it contains (e.g., 02-10, 02-11, etc.). In cell A2 of each worksheet is a number, and I want to write a formula that looks at cell A2 in every worksheet, and returns the largest one.
I created a list of the current worksheets and named it DayWorksheets, and I attempted to get the value using the formula
{=MAX(INDEX(INDIRECT("'"&DayWorksheets&"'!A2"),0))} (brackets shown to denote array formula).
But the result every time is "10," which is the number in cell A2 of the first worksheet in the named range (and it's not the largest). What am I doing wrong here? I've seen a few other formulas around, the likes of
=VLOOKUP(C1,INDIRECT("Sheet"&MATCH(TRUE,COUNTIF(INDIRECT("Sheet"&ROW(INDIRECT("1:10"))&"!A2:A100"),B1)>0,0)&"!A2:B100"),2,0),
but I don't quite understand the purpose of the countif function in there. Any help would be appreciated.
You can use formula with 3D-reference like this:
=MAX(Sheet1:Sheet30!A2)
Click the cell where you want to enter the function.
Type = (equal sign), enter the name of the function (MAX), and then type
an opening parenthesis.
Click the tab for the first worksheet that you want to reference.
Hold down SHIFT and click the tab for the last worksheet that you
want to reference.
Select the cell or range of cells that you want to reference (A2 in your case).
Complete the formula (add closing parenthesis), and then press ENTER.

referencing sheets by number instead of name in cells

Lets say
sheet3.name = "d"
Is there a way I could put in a cell on sheet2 the formula =sum(sheet3!b:b) where sheet3 is being substituted with the actual sheet3 name?
I can only get =sum('d'!b:b) to work so far.
I could use VBA for this probably but I'm curious how to do this in a cell so I don't have to run a macro each time.
If you can use a UDF User Defined Function that will return the sheet name
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function
then a formula like
=SUM(INDIRECT(SHEETNAME(3) &"!B:B"))
will return the sum from column B on sheet 3.
SHEETNAME(number) returns the sheet name of the number which is index.
So Sheet(1) returns Sheet1, etc
Use below formula anywhere in the sheet to get the sheet name - the sheet must have a filename for this to work:
=REPLACE(CELL("filename"),1,FIND("]",CELL("filename")),"")
You can either reference that cell using Indirect:
=SUM(Indirect("'"&A1&"'!B:B"))
or, if you don't want to have a second cell, you can combine the two formulas into one:
=SUM(INDIRECT("'"&REPLACE(CELL("filename"),1,FIND("]",CELL("filename")),"")&"'!B:B"))
For anyone not concerned with the order of the sheets, the post by Biff here on mrexcel.com works well.
In Excel 2013, go to the Formulas tab in the ribbon and make a defined name:
Name: SheetNames
Refers to: =GET.WORKBOOK(1)&T(NOW())
Then use a formula like this example:
=INDIRECT("'"&INDEX(MID(SheetNames,FIND("]",SheetNames)+1,255),A3)&"'!A1")
where A3 refers to the index number in a cell in the current sheet, and A1 refers to the location of the value to be retrieved from the other sheet. I.e., in the current sheet, if A3 = 2, then the formula will point to cell A1 in the second sheet of the workbook. I just use a column of index numbers in my current sheet, then drag this formula down and it fills in values from all of my other sheets.
You will need to save as a macro-enabled file (.xlsm).
I'm not sure if this is a good idea but it's the first one I could think of.
I would add additional function to your VBA project which will return actual name of your Sheet3:
Function Sheet3Name()
Sheet3Name = Sheet3.Name
End Function
Next, when you create sum formula of column B:B in Excel cell you need to do it in this way:
=SUM(INDIRECT(Sheet3Name()&"!A:A"))

Unsure of how to use cell data to get data from another worksheet

This is a weird sort of function that I've never tried to use before, so I'm not sure how to go about asking without using an example.
Essentially, I have a column of strings that represent datasheets in this .xls file. Each row has data in it that corresponds to the datasheet named in said column. In case my explanation is insufficient, here's an example:
Worksheet 1 : Worksheet 1 Value 1 : Worksheet 1 Value 2 : .......
Worksheet 2 : Worksheet 2 Value 1 : Worksheet 2 Value 2 : .......
etc...
At the moment, the spreadsheet requires you to manually update the worksheet reference for each column to populate the data, i.e. for every cell, I need to manually fill out the worksheet name in this equation:
='Worksheet 1'!B111
This works, but is obviously suboptimal, as I have a large number of worksheets. What I WANT to do is be able to do is be able to fill out Cell 1 (the worksheet name) and have every other cell use the worksheet name as its reference, like so:
Column A : Column B : Column C : ...
"Worksheet 1" : =A1!B111 : =A1!B34 : ...
"Worksheet 2" : =A2!B111 : =A2!B34 : ...
First question: Is this possible?
Second quesion: How?
Sorry if this has been answered already, but I could barely figure out how to ask the question in the first place.
You can use the INDIRECT() function. With the text Worksheet 1 in cell A1 you can use
=indirect("'"&$A1&"'!B111")
Since the sheet name can contain spaces, you need to start the text of the Indirect with a single quote, append the cell with the sheet name, then append the closing single quote, the exclamation mard and finally the cell reference.
If all cells in column A refer to B111, just copy the formula down and change the sheet name in column A. Copy the formula across and adjust the cell reference for B111 to whatever is desired before copying down.
If you want to pre-populate a worksheet with these values, fire up the macro recorder, create a new sheet, go to an existing sheet and copy and paste everything into the new sheet, then stop the macro recorder. That will give you a starting point for the code you need.

Resources