referencing sheets by number instead of name in cells - excel

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"))

Related

Excel - Identify if Worksheet exists between two worksheets?

I have 5 worksheets: (1) Summary (2) 2020 (3) 2019 (4) 2018 (5) 2017
In my "Summary" worksheet, I have Column A with cells each containing the name of each worksheet (A1: 2020, A2: 2019, A3: 2018, A4: 2017) and Column B with the number "1" in each corresponding row.
Let's say I delete the "2019" worksheet so that it is no longer between worksheets "2020" and "2017" - is there any formula that I could use to flag that in row 2? Perhaps through some sort of "if(..,1,0)" formula for Column B? I do not want to use VBA.
There are various error-checking functions available in Excel, and most of them would be appropriate for your situation. The simplest would be to use IFERROR and just refer to a cell on one of the sheets, like this:
=IFERROR('2020'!A1,"Missing")
If the formula receives an error value because it can't find the cell, it will display the text "Missing"; otherwise, it will show the value of the cell.
The disadvantage of doing it this way, hard-coding the sheet name into the function, is that if the sheet is deleted the sheet name will be replaced in the formula with #Ref!, so even if the sheet is restored the formula will need to be changed.
We can enhance the functionality by using an INDIRECT along with the IFERROR. This will take a text string and turn it into a cell reference. So assuming we have worksheet names in column A, we could do this:
=IFERROR(Indirect(A1 & "!A1"),"Missing")
The INDIRECT function takes the value in A1 and combines it with the text string, and reads the whole thing as a cell reference. This has the same result, but is more robust since deleting the sheet and then replacing it will clear the error.
For a slightly more elaborate result, we could next IF and ISERROR instead of using IFERROR. It gives us more control over the result.
=IF(ISERROR(INDIRECT(A1 & "!A1")),"Missing","Found")
ISERROR returns TRUE if the first argument throws an error, an FALSE if it does not. This will return "Found" if the sheet is present, or "Missing" if it isn't.

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?

Excel - Reference within a reference

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.

excel return formula with relative cell

I have 2 worksheets in my excel, the first sheet allows me to select a calculation method from a drop down list and input the variable for the calculation (shown in green cell, the column in blue shows some constant number). The result entry will search for the corresponding calculation formula from my second worksheet (database), then paste the formula to the sheet 1, I need the formula to calculate using the cells in sheet 1 instead of cells in my database.
currently I created an user defined function called Eval as below:
Function Eval(ref As String)
Eval = Application.Evaluate(ref)
End Function
by combining the Eval with vlookup :=Eval(VLOOKUP(A3,Database!A2:E10,5,FALSE)) I will get the result that the calculation equation uses the cells from my database, how can I achieve the result which the formula takes cells in sheet 1 during calculation?
One simple way would be to use all formulas within one CHOOSE like this:
=IFERROR(CHOOSE(SUMPRODUCT(MATCH(A2,"Calculation "&ROW($1:$9),0)),B2*C2*D2,B2*C2-D2,B2+C2-D2,B2^2-C2+D2,B2*D2-C2^2,B2+C2*D2,B2*C2-C2*D2,B2-D2-C2*D2,C2-D2*B2),"")
Another would be to use the Application.Caller like:
Public Function eval(ref As String)
eval = Application.Caller.Parent.Evaluate(ref)
End Function
This ensures the use of of the parent of the caller (the sheet with the eval() formula) to be used as main-ref.
EDIT
Keep in mind that your "rows" are static in the formulas. Going for "Calculation 4" will use row 5 (and not the row of your formula from the first sheet). For this you could use something like:
=Eval(SUBSTITUTE(VLOOKUP(A3,Database!A2:E10,5,FALSE),"##",ROW()))
While all row-numbers should be changed to ## (or whatever unique identifier you like). Then "Calculation 1" would look like: =B##*C##*D##
If you still have any questions, just ask :)

Excel - Selecting from cell A1 to AX, where X is read in cell B1

I have an Excel with 2 worksheets, first will be dinamically filled with data, having a row with a combo box feeding from row A on the second worksheet.
The second worksheet will also be filled dinamically, where will be:
Row A:
some values of variable number
B1 - Number of values on Row A to considerate.
My question is - Im using Data Validation > List to define the values on the ws1rowA combo box, is it possible to range from A1 to A(value in B1) ?
So far tried this on Data Validation "source" field:
=Sheet2!$A$1:offset(Sheet2!$A$1,=Sheet2!$B$1,0,1,1)
but an error is returned
You can also use INDIRECT function for this.
=INDIRECT("Sheet2!$A$1:$A"&Sheet2!$B$1)
In my version of Excel "You cannot use references to other worksheets or workbooks for Data Validation criteria" but you can use named ranges that have a Workbook Scope, so name a range (eg DataValid) to apply to say the range as #Maxim Korneev and then for Data Validation in Sheet1 use a list whose Source: is =DataValid.
Sure, just use
=OFFSET(Sheet2!$A$1,0,0,Sheet2!$B$1,1)

Resources