How do you add a variable to an excel function? - excel

I have an excel file with multiple sheets. I reference data in sheet 2 like:
=sheet1!A1,
I would like to use a variable to change the function based on a value entered in cell x on sheet2 . I tried:
=sheet1!"x"1
to try and change all formulas based on entering a value for x. This didn't work because excel thought I was referencing something in sheet 1. I can't do this with VB due to security restrictions.

Use the indirect() function:
=INDIRECT("'Sheet1'!" & A1 & "1")
Where A1 is the cell in which you find "x".

The best way to do this is by using =INDIRECT("'Sheet1'!" & A1 & "1")
EDIT: Dang I'm too slow, Scott is like lightning.

Related

Transpose 'Sheet1'!A1 with B1!A1

I have the sheet, say "StackOverflow", and a value in cell A1, "Hello", that I want in my sheet "Test" at a specific cell.
I know that I could type ='StackOverflow'!A1 to get the cell value. The thing is that I do not always want to use the sheet "Stackoverflow" and instead want it to be like
=B1&!A1
where B1 holds a value that corresponds to a worksheet in my workbook.
How would you go about this?
You can use the INDIRECT function :
=INDIRECT("Stackoverflow!A1")
or
=INDIRECT(B1&"!A1")
=INDIRECT(B1 & "!" & A1)
Note that the above allows you to place any cell reference in A1.
If you want it to always refer to A1, then use
=INDIRECT(B1 & "!A1")
=INDIRECT(B1&"!"&A1) would do this.

Excel Formula - Check if cell has formula

What formula do you use to check if another cell has formula? For example, I have 2 columns, A has cells which contains either a formula or a value.
(Column A usually contains Formulas but other users try to change their values by directly typing and replacing the formula that was previously there)
In Column B I want to add a formula that will say "HasFormula" if the cell on Column A has formula and say "PlainValue" if it contains a value.
I'm thinking maybe using =ISNUMBER() but that may not be accurate.
I am using Excel 2010.
Excel actually has a builtin ISFORMULA() function.
Say A1 has a formula and you want to check that. In say B1, you can use:
=If(ISFORMULA(A1),"HasFormula","PlainValue")
Edit: Per your comment, you don't have ISFORMULA(). An alternative is to create a quick UDF, and use the custom function in the worksheet.
In a workbook module, put this code:
Function isFormula(ByVal target As Range) As Boolean
isFormula = target.hasFormula
End Function
Then you can call it like this: =isFormula(A1) and it will return TRUE if A1 has a formula.
If you can't use VBA, then you can use this formula:
=IF(ISERROR(FORMULATEXT(A1)),"PlainText","HasFormula")
The MrExcel website (link below) has this method which uses old code from Excel 4 (which is still present for backward compatibility)...
Define a NAME such as "CellToLeftHasFormula" and in the "refers to" box put
=GET.CELL(48,OFFSET(INDIRECT("RC",FALSE),0,-1))
Then in column B use the formula =CellToLeftHasFormula which will return TRUE if it has.
Be aware that this will mean your Excel will now contain a macro and so will need to be saved as such (xlsm). I use this in Excel 2010.
For full explanation (and other .CELL options, besides 48) see MrExcel link: https://www.mrexcel.com/forum/excel-questions/20611-info-only-get-cell-arguments.html
You can use the Range.HasFormula property.
https://learn.microsoft.com/en-us/office/vba/api/excel.range.hasformula
EDIT:
Text and code from the above link:
"True if all cells in the range contain formulas; False if none of the cells in the range contains a formula; null otherwise. Read-only Variant. ..."
Worksheets("Sheet1").Activate
Set rr = Application.InputBox( _
prompt:="Select a range on this worksheet", _
Type:=8)
If rr.HasFormula = True Then
MsgBox "Every cell in the selection contains a formula"
End If
You can restrict the user by protecting the column A.
You can directly check if a cell contains a formula by using a shortcut Ctrl + `.
You can use vba and write a user defined function :
1. Press alt + F11
2. Insert module in workbook
3. Paste this code
Function IsFormula(cell_ref As Range)
IsFormula = cell_ref.HasFormula
End Function
4. Now, use Isformula in the cell wherever you want.

How to refer the value of a cell in excel into a formula

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.

Take values from another spreadsheet, based on a value on the original workbook

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.

Formula in Excel that references another Excel file based on cell reference

I want to make a formula that references a cell in another excel file. I have figured that out as below:
='C:\Users\17\Desktop\[JAN-11 2011.xlsx]1'!$H$44
But as the cell L1 in this worksheet is Data cell set to month so it show current month so I want to be able to do something as below:
='C:\Users\17\Desktop\[&L1& 2011.xlsx]1'!$H$44
but this is not working. How do I get this function to work off of the cell L1?
I think you could use Lance Roberts suggestion, but include it in a INDIRECT() function, like this: =INDIRECT('C:\Users\17\Desktop[' & L1 & ' 2011.xlsx]1'!$H$44)
Try using string concatenation like so:
='C:\Users\17\Desktop[' & TEXT(L1, "mmm-dd") & ' 2011.xlsx]1'!$H$44

Resources