I'm trying to figure clean up a spreadsheet - but I'm trying to avoid typing out a lot of hardcoded values that are referenced in formulas.
So right now, I have a function that takes a string
=FunctionThatWantsAString(A1,SomeOtherInputs)
In Cell A1 I have "SomeString"
Is there a way to easily replace the cell link "A1" in the formula with the string "SomeString"?
Thanks
The simple solution is to go to the Formulas ribbon > Defined Names > Name Manager > New [or simply select A1, and then click on the NameBox which says "A1" and type in "SomeString"]
This allows you to name a specified range. You can then refer to that name either within an excel worksheet or within VBA.
Edit for additional request
If you don't want A1 to have to hold the text "SomeString", you can actually use the name manager to create a name which refers to a string. ie: you can have the Name SomeString be equal to "asdf". Then, use ctrl+f to find and replace all instances of "A1," in your worksheet with "SomeString".
Alternatively , just use ctrl+f to find and replace "A1" with ""asdf"", and have your results hardcoded directly in all cells. Probably not recommended to do that though, for maintenance purposes.
If you are using a User Defined Function, you should check if that first parameter is a cell value, and if so, get the value.
Public Function test(st As Variant)
If TypeName(st) = "Range" Then
'get the value of the range or use the string passed in
'or if TypeName(st)="String"
End If
test = TypeName(st)
End Function
Related
Similarly to this thread I have found Click here...
I am trying to create various formulae looking across tabs with the tab names kept in cells. My hyperlink function has been successful as:
=HYPERLINK("#'"&B2&"'!A1","Click Here")
Where B2 represents a 2-3 character tab name of a person's initials (e.g. AA in this example).
However if I try this method with other formulae I am returning a #VALUE! error. Can anyone help me with making this nested Index/Match function work dynamically from cell B1 rather than being fixed to the tab name "AA"?
=IF(OR(INDEX(AA!B:AH,MATCH(TODAY()-WEEKDAY(TODAY(),11)+1,AA!B:B,0),2)="",INDEX(AA!B:AH,MATCH(TODAY()-WEEKDAY(TODAY(),11)+1,AA!B:B,0),10)="",INDEX(AA!B:AH,MATCH(TODAY()-WEEKDAY(TODAY(),11)+1,AA!B:B,0),14)="",INDEX(AA!B:AH,MATCH(TODAY()-WEEKDAY(TODAY(),11)+1,AA!B:B,0),22)=""),"No","Yes")
Thanks in advance?
Dan
The hyperlink function accepts a constructed string to use as the link and interprets it as a range address just as it would a true url. A formula cannot accept a constructed string address as a worksheet range reference but the INDIRECT function converts constructed strings to a usable worksheet range reference.
INDEX(AA!B:AH,MATCH(TODAY()-WEEKDAY(TODAY(),11)+1,AA!B:B,0),2)
... becomes,
INDEX(indirect(text(B2, "'#'!\B\:\H")), MATCH(TODAY()-WEEKDAY(TODAY(), 11)+1, indirect(text(B2, "'#'!\B\:\B")), 0), 2)
With AA in B2, text(B2, "'#'!\B\:\H") becomes 'AA'!B:H. I find it easier to take care of the wrapping ' marks with a format mask.
it is possible to declare a variable in excel lets say N=10, then if i type letter N in a cell and that cell makes part of a sum, that cell will be counted as 10?
Go to Formulas > Define Name there define your variable.
Then open VBA with Alt + F11
Insert a module and there use this function:
Function EVAL(rng As Range) As Variant
EVAL = Evaluate(rng.Value)
End Function
You can use the function in a formula: =EVAL(Cell where is your var N)
You can name a cell by selecting it, clicking the field with its address (in upper-left corner) and entering name (in your example "N"). Value in this cell is value on your variable. Good idea is to make separate sheet that will contain variables (you'll avoid mess), but it's not necessary.
And then you can use it in formulas, f.e.: =A1*N. But remember, if you want just fill cell with its value, use =N instead of N.
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.
I'm using two cells with Data Validation - the first cell (E9) simply creates a drop down menu based on the range A2:A6, and the second cell (E10) validation uses the source INDIRECT(E9), which will always refer to one of five different named ranges.
When I have the named ranges fixed, (i.e A2:A250), the second drop down works, but I really need the ranges to be dynamic, so far I've been creating named ranges with the following "source" formula:
=OFFSET(LookupLists!$B$2,0,0,COUNTA(LookupLists!$B:$B),1)
With the other ranges being the exact same only in columns C-F.
When I write out this formula it highlights the correct area on the screen, but the drop down button in cell E10 is completely unresponsive, when the drop down list should show the exact area that's being highlighted.
As a note, the lists themselves are created using an array formula and some VBA code to create a sorted unique list based on another part of the spreadsheet, so I've been unable to use tables to create the ranges as some other websites have suggested.
INDIRECT doesn't work with dynamic ranges. Credit to these guys for the solution:
http://chandoo.org/forum/threads/passing-a-named-range-to-a-formula-with-indirect.5854/#post-32423
First, insert a module into you sheet and paste in the UDF:
Option Explicit
Function RetrieveRangeForName(psRange As String) As String
RetrieveRangeForName = Range(psRange).Address
End Function
Then you will need a helper cell, since I don't think UDFs work in the Data Validation dialog. In E11, enter =RetrieveRangeForName(E9).
Then in the Data Validation, set to List, you can enter: =INDIRECT(E11)
The reason it doesn't work as discussed here is that INDIRECT expects a string that it can evaluate to give a reference. However your named range is already a reference. Logically, the only way to use it in INDIRECT is to convert it into a string first which you can do with a UDF:-
Function GetAddress(Name As String) As String
Dim rng As Range, addr As String
Set rng = Worksheets("Sheet1").Range(Name)
addr = rng.Address
GetAddress = addr
End Function
Then use this to define a range called NewRange:-
=INDIRECT(GetAddress(Sheet1!$E$9))
Finally this can be used in the validation for E10 (Named Range ListB is defined as in the question, ListA etc. correspondingly for columns A to E).
Currently I have the following set up (simplified):
With the formulae:
How would I change this to make the result in A7:E7 (e.g. Google) into a hyperlink to the address stored in the result's reference cell (e.g. "www.google.com" in cell A2)?
Many thanks,
Alex
Following the scheme :
Using EXCEL Function:
you can split in two columns the Hyperlink and use in the formula of Result:
=HYPERLINK(INDEX(B2:B4;D2);INDEX(A2:A4;D2))
Using VBA:
In a module add the Function:
Public Function Addr(x As Range) As String
Addr = x.Hyperlinks.Item(1).Address
End Function
and in the Result use:
=HYPERLINK(Addr(INDEX(A6:A8;D2));INDEX(A6:A8;D2))
Using Name Manager:
In the name Manager (Under menu Formulas) add a name called "KKK", inserting:
=GET.FORMULA(INDEX(Sheet1!$A$12:$A$14;Sheet1!$D$2))
and in the result cell:
=HYPERLINK(MID(KKK;1+FIND("""";KKK);FIND(";";KKK)-(2+FIND("""";KKK)));INDEX(A12:A14;D2))
Work ONLY if it's a FORMULA, Don't work if are a Link like A6.