I have a drop down list (with name of sheets) and based on that value, let's say that I select the Sheet4 as in the image, I want to bring to another sheet the value of that selection, let's say on the cell B8.
I know that this works:
=IF(B1="Sheet1", Sheet1!B8, IF(B1="Sheet2", Sheet2!B8, Sheet3!B8))
That's for just 3 sheets but is there a nicer or more efficient way to do this?
This is in general how all the sheets look like:
Use INDIRECT to construct a valid worksheet and cell reference from text-that-looks-like-a-worksheet-and-cell-reference 1
The indirect takes a string and turns it into a valid reference.
=INDIRECT("'" & B1 & "'!B8")
So in the case above it would create a string "'Sheet4!B8". Then the Indirect will turn it into a valid cell reference.
As Jeeped also pointed out in the comments The B8 reference since it is literal text it will not change if the formula is copied or dragged to another cell.
The B1 which is a cell reference and is relative will change as it is copied or dragged to different cells.
1 as per #Jeeped comment
Related
My formula is as follows:
=(CONCAT("=RSLINX|PLC1!",B3)
B3 = VarName1
The goal is to use the RSLINX function with the contents of a changing cell (B3)
I have also tried putting "=RSLINX|PLC1!" in it's own cell to reference as a string, as well as the following formula:
=INDIRECT(CONCAT("=RSLINX|PLC1",B3))
The objective is that B3 can change to a different cell (B4, B5, etc.) so that I can evaluate many different tags quickly, as the only way I've had the RSLINX function work is by manually typing in a correct tag name.
Resolved. I had to use the original formula:
=(CONCAT("=RSLINX|PLC1!",B3)
For all desired tags, then copy it and use "Paste Value" and Search & Replace all "RSLINX" with "=RSLINX" in the new columns.
I want to use the value of a cell in a reference formula.
For example:
I have a sheet with name '815108' which has all the data I need.
Now in the sheet1(new sheet), in A1 I type in ='815108'!A3 which gets the data from A3 in the sheet '815108'.
But the question I have -
In my new sheet 815108 is a defined attribute (For example: SO = 815108 is defined in cell F5.)
Instead of using '815108'!A3 I want to use the location in the current sheet. I tried ='=F5'!A3 which doesn't work. Any help is appreciated.
Thank you.
This is one of the few times that the INDIRECT should be used:
=INDIRECT("'" & F5 & "'!A3")
INDIRECT is a volatile function that translates a string into a viable reference.
Being volatile it will re-calculate every time Excel re-calculates regardless if the data to which it refers has changed or not.
So I have that table above, I use Excel VBA to add new prices then add the formula to Decision column.
As you can see, cell B2 formula should be =IF($A2>50000,"Ignore","Buy") and cell B3 formula should be =IF($A3>50000,"Ignore","Buy") so the formula in B2 refers to the value in A2, this is the same for B3 to A3 and so on. I use the VBA below to add the same formula to blank cells. Yes, there will be blank decision cells and they need formula. I must NOT use autofill from top to bottom. I tried using below (LastRow is the usedrange.row):
Sheet1.Range("B2:B" & LastRow).SpecialCells(xlCellTypeBlanks). _
Formula = "=IF($A2>50000,""Ignore"",""Buy"")"
The problem with that VBA is even in cell B5 the formula is =IF($A2>50000,""Ignore"",""Buy"") when it should be =IF($A5>50000,""Ignore"",""Buy"") (should be $A5 instead of $A2). What am I doing wrong?
With SpecialCells(xlCellTypeBlanks) you will probably get a non continuous range. With this the auto fill process will not work with A1 formulas. But with R1C1formulas it will.
Use:
.Range("B2:B" & lastrow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=IF(RC1>50000,""Ignore"",""Buy"")"
RC1 means the Row you are currently in but always fix Column 1.
For R1C1 references see https://support.office.com/en-us/article/Overview-of-formulas-7abfda78-eff3-4cc6-b4a7-6350d512d2dc?CorrelationId=2bedf5ef-a3b7-4a82-9b12-6ee86b494ae9&ui=en-US&rs=en-US&ad=US#bmusing_references_in_formulas. Scroll down to The R1C1 reference style.
You can paste the formula in all cells, considering you have the formula in cell "B2":
Range("B2").Copy
Range("B2:B" & LastRow).PasteSpecial xlPasteFormulas
edit for more detail: You can use the R1C1 reference style, more importantly, R[1]C[1] notation. There is a caveat for different languages though, see the very end of the post. Examples:
R2C4 'row 2, column 4 so it's the cell D2 in A1-notation
R[2]C[4] 'the cell 2 to the right and 4 down from the current cell (where this reference is located)
R[2]C4 'the cell 2 to the right from the current cell in column 4 (D)
R[-2]C[-4] 'you can also give negative arguments, this is the cell 2 to the left and 4 up
R[2]C 'the same as R[2]C[0]
RC[4] 'the same as R[0]C[4]
R2C 'the same as R2C[0]
RC4 'the same as R[0]C4
R2 'row 2
C4 'column 4 (the same as D:D)
As you can see from the last three examples, the notations can't be mixed.
Now for your case:
If you want to have the following in cell Bx (replace x by any number)
"=IF($Ax>50000,""Ignore"",""Buy"")"
This would be the R1C1 formula
"=IF(RC1>50000,""Ignore"",""Buy"")"
or if it is more important that it is the column to the left:
"=IF(RC[-1]>50000,""Ignore"",""Buy"")"
The latter would be the like dropping the $ from the original formula.
Your second formula was
"=IFERROR(VLOOKUP(RC3,Database!$A:$F,3,FALSE),""Missing"")"
and Axel's answer
"=IFERROR(VLOOKUP(RC3,Database!C1:C6,3,FALSE),""Missing"")"
should be clear now.
If you don't want or can't use the formulaR1C1 property but still use the R1C1 style reference for a single cell, you can use the INDIRECT worksheet function. INDIRECT("R1C1",FALSE) is a reference to R1C1. The FALSE tells it to use R1C1 instead of A1 notation. It might behave slightly different than a simple reference if there is something other than numbers in the referenced cell.
I personally like the R1C1 notation better than the A1 notation mostly because it is easier to reference cells relative to the current position but also because it is easier to read for high column numbers and it's closer to the Cells(rowIndex,columnIndex) syntax.
One last thing: In other language versions of excel, R1C1 might be named differently. That doesn't affect the formula when you enter it via VBA (I think) but if you want to enter it from the worksheet, you need to keep that in mind. In German it's Z1S1 for example. This can also cause problems when opening the file with a different language version. If you used INDIRECT("R1C1",FALSE) in a formula, the INDIRECT and FALSE will be translated but the string will not so it will not work :( (The last part is from memory)
I'm looking automatically to calculate the average of 40 values that come from a .csv file, which I have managed to do.
My problem is I would like it to take the values from any separate workbook based on the cell value from the original workbook, so this can automatically calculate the average from any one of a number of available spreadsheets.
This is the formula I'm using currently:
=Sample1.csv!C1
And this is the what I tried, which obviously doesn't work:
=B4.csv!C1
With B4 containing the word Sample1 or any other Sample Number.
The formula you can use to refer to a cell on another sheet
=INDIRECT(B1 & "!C1")
Where B1 has the sheet name and you get the value of cell C1 from it.
It can be used to get data from another workbook , so long as the other workbook is open.
eg
=INDIRECT("[Book1.xlsx]a.csv!$A$1")
=INDIRECT("[" & B1 & "]!$A$1")
Where B1 has the name of the workbook
In practise you might use:
=INDIRECT(B1 & "!" & C1)
Where C1 contains (ie stores as a value!) the address of the cell you want to reference. Thsi is useful as it allows you to copy the formulas such that the addresses change.
It is better to use the CELL function as follows which uses CELL to get the Address of A1 as text eg "$A$1". This means the formula can easily be copied and pasted into the cells you want it to be in.
=INDIRECT(B1 & "!" & CELL("address",A1)
Enjoy.
You can use the Indirect() function to have Excel interpret a text string as a cell reference, and this string can be constructed with the text values from one (or more) cells.
So in your example use:
=INDIRECT(B4 & "!C1")
which retrieves the value "Sample1" from B4 and concatenates this with the second part of the string to give "Sample1!C1" that Indirect() then uses as an address & retrieves the value from there.
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"))