I'm trying to create a formula that will take the contents from a cell on a different sheet. I don't want to just give the direct cell reference, because I want to be able to cut & paste this into different places and refer to a reference number to know which cell to get.
So, I'm using the Cell() function. But, when I enter something like =CELL("contents", Sheet1!A2), it returns the value of A2 on the current sheet, not on Sheet 1. Every other function I've tried recognizes Sheet 1 when it is given. (This example is just a trial. In reality I have to use Concat() & Indirect() to create the cell reference).
If it matters, I'm using Excel365
If I manually reference a cell in another spreadsheet (by typing = in the destination cell and then clicking in the source spreadsheet), excel creates a formula like this: ='H:\excel\data\[source_file.xls]Sheet1'!$C$5
I am wanting to create that formula from the cell and path which are stored in cells in the destination spreadsheet (and some strings).
So I have in 3 cells B1:B3 in the destination spreadsheet these strings:
H:\excel\data\
source_file.xls
Sheet1!$c$5
Then, so far, I have placed this formula in the cell where I want the data from the source spreadsheet:
=B1&"["&B2&"]"&B3
Which evaluates to:
H:\excel\data\[source_file.xls]Sheet1!$c$5
Pretty close to what I need. But as you can see some single quotes are missing. If I try to put these in, excel seems to think I am giving it a defined name.
Is there perhaps some sort of escape character I should put before the quote to make it work?
I have searched this website and excel help files to no avail.
That is the end of the question but I anticipate that someone is going to ask me why I am doing this. Well I want to automatically pluck values from a directory full of spreadsheets (which all have the same format)the value of one cell from each.
I had tried to do it using the advice found here:
http://www.ashishmathur.com/extract-data-from-multiple-cells-of-closed-excel-files/
Unfortunately, I was only able to get the first step to work (create a list of filenames of the files in the source directory). The remaining steps are not possible because my employer does not allow downloading of add-ins for security reasons (it is a hospital).
So I thought the approach outlines above might work but I can't quite make it happen!
Thanks
Mike A
Use INDIRECT()
=INDIRECT("'" & B1 & "[" & B2 & "]" & B3)
As stated in the comments the ' would need to added to B3, so the input is Sheet1'!$C$5.
You may want to split the sheet name from the cell address then you could do:
=INDIRECT("'" & B1 & "[" & B2 & "]" & B3 & "'!" & B4)
Where B3 is only the sheet name and B4 the cell address.
I am trying to further my knowledge in excel, I have searched around all day for an answer to my current question but I was unable to find anything relating to my needs.
I basically, want to know if it is possible to reference which column a formula should use, by storing it in a cell.
So if I have a formula which is using column - Test!C:C, am I able to store that string in a cell and reference that cell in the formula? I have many formulas which are using the same reference, so if i decide to change what column i want to utilize, it takes some time to remove them all.
I know i can use replace all, but it would be fantastic if there was a method to reference a column via a cell.
Use the Indirect Function:
If the cell in which you put the column Address "Test!C:C" was A1, you would use:
=INDIRECT(A1)
If the sheet never changed and you only wanted to reference the column. So in A1 you only want to put "C":
=INDIRECT("Test!" & A1 & ":" & A1)
Indirect lets you enter a string that is then used as a reference.
There is one major draw back to the INDIRECT() function, it is volatile. This means that it will calculate every time excel calculates, not only when the reference cells change. So depending on the number of formulas, it will slow the calculation times.
I have a workbook with different sheets for each month. I made another sheet where I put the months in A1 down.
I wanted to use Counta (for A:A in the months' sheets) and indirect together but I am getting the result "1" with my formula.
Please help:
=COUNTA(INDIRECT(SheetNames!A1&"!A:A"))
I would also eventually like to do the same for countif. If you can please explain what I missed that will help me.
Thanks!
Trying out your formula it works perfectly unless you've got a space in the worksheet name.
Try this formula:
=COUNTA(INDIRECT("'" & SheetNames!A1 & "'!A:A"))
When a sheet name contains a space then the sheet name must be enclosed within apostrophes. So =COUNTA('Sheet 4'!A:A) is a valid formula, while =COUNTA(Sheet 4!A:A) will not work and causes all kinds of problems.
The INDIRECT formula needs a valid reference as a string (rather than an actual reference) to calculate - so "'" & SheetNames!A1 & "'!A:A" returns the string 'Sheet 4'!A:A.
How does one cell obtain the formula of another cell as text without using VBA? I can see this question has already been asked many times and the answer is always to write a custom function in VBA.
However, I found a post made in 2006 which claimed to have found the non-VBA solution but the link provided in that post is already broken.
=FormulaText(Reference) will do the trick Documentation
There is nice way of doing this without VBA. It uses XL4 macros (these are macros, but it is not VBA, as asked).
With reference to the figure 1, cells A2:A4 contain usual formulas.
Going to Formulas -> Define Name, I defined two named ranges (see fig. 2), with the information shown in cells A6:B8.
Enter in cell B2 =FormulaAsText. This will retrieve the formula in cell A2 as text.
Explanation:
The named range FormulaAsText uses =GET.CELL(info_type,reference). In this case, ìnfo_type = 6 retrieves the formula, and reference = OFFSET(INDIRECT("RC",FALSE),0,-1) uses the cell with 0 rows and -1 columns offset from the one the formula is used in.
Copy B2 and paste into B3:B4. This will show formulas in A3:A4. Cell A4 shows that the worksheet function CELL only retrieves values, not formulas (as opposed to GET.CELL).
Since FormulaAsText gets the formula from a cell at fixed offset (0,-1) from the current, I defined another range FormulaAsText2, which uses an offset (rows,cols) read from the worksheet itself. Cells D2:D4 contain =FormulaAsText2. Thus, cell D2 shows the contents of cell B3 (=OffSET(D2,1,-2)), which is FormulaAsText. cells D3:D4 show the contents of themselves. This adds some flexibility. YMMV.
PS1: The essence was taken from
http://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
PS2: Tim Williams mentioned in a comment "the old XLM GET.FORMULA()". This answer is possibly related (not the same, since this one uses GET.CELL()).
PS3: A simple VBA solution is given, e.g., in
http://dmcritchie.mvps.org/excel/formula.htm
EDIT: Complementing this nice answer, the worksheet function FormulaText is available for Excel 2013 and later.
This suggestion may be helpful for those who after retrieving a block of formulas and transporting them to a new spreadsheet want to put them to work again. Excels FORMULATEXT function is great for picking up formulas but it leaves them as unusable text strings. If you want to get them back as fully functioning formulas you have to edit each one individually to remove the string character, but here is a shortcut for larger blocks.
Get to the position where you have the required formulas as text (in other words after using FORMULATEXT - you have done a copy and (value only) paste). The next step involves highlighting all the cells you want to convert and then navigating to the [Text-To-Columns] menu option ({Data} bar on Excel 2016). You can select 'Delimited' but on the next screen just make sure you de-select any marks that do appear in your formulas. Then 'Finish'. Excel should automatically analyse the cells as containing formulas and you should now have them working again.
There is a way to do this. In my example I had a table that showed a date. The date comes from Sheet!G91. In my table I also had a column that showed the sheet name. I added two more columns to my table. The first column had column(Sheet!g91), which returns the number 7, because G is the seventh letter in the alphabet. I then converted the number to a letter (G) using another table in my workbook. In the second column that I added, I made a formula row(Sheet!G91), which returns the number 91. Note: Row and Column may appear as volatile formulas, which recalculate with every calculation of the workbook.
I wanted another column to show the formula contents of the date cell mentioned at the beginning of this post. I included the following string function (you can also use CONCATENATE).
"=" & AJ9 & "!" & AM9 & AN9
The items separated by ampersands get strung together (that is, concatenated). AJ9 in my example contains the sheet name, AM9 contains the column letter, and AN9 contains the row number.
I now have a column that dynamically updates its contents to reflect the sheet name and cell reference. The results in my workbook cell are
=Sheet!G91.
You can't. This is most likely a design choice to eliminate an average Excel user from accidentally getting something they did not want.
What you are reading is correct - writing a UDF is the solution you want.