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.
Related
Sorry if this is a super basic VBA question but I am very new to Excel. But suppose I have an input cell E6, where I can just put any number into it. I also have a cell S12, and its formula is defined in the worksheet and is complex (depends on ~15 other cell values, including E6). How can I write a VBA function that takes in one input X, put it into E6, and returns the value of S12?
I tried doing this
Public Function Neville(X As Double) As Double
Range("E6").Value = X
Neville = Range("S12").Value
End Function
but it is giving me value errors. I think it might be because changing the value of a cell in VBA does not make the value of other cells in the worksheet change?
I have a cell (A1 which have the text "3+2". On the next cell I need to put a formula which is "=A1+1".
How can I tell excel that it has to do the sum on A1, so he can sum another values?
Try to use EVALUATE function. It is best way to do it without VBA, however needs simple trick:
While being in cell B1 add to name manager range "Result" with formula
=EVALUATE($A1)
Than just place in B1 formula
=Result
For more comprehensive description regarding EVALUATE function please refer to this link. Additionaly please notice that EVALUATE function can have different name in your local language.
Btw. similar topic was discussed here
If A1 contains something like
number+number
then in B1 enter:
=LEFT(A1,FIND("+",A1)-1)+MID(A1,FIND("+",A1)+1,999)+1
If A1 contains an arbitrary numeric expression as text, you would use a VBA UDF.
EDIT#1:
If you have a list of values separated by the + sign, then you can use this array formula:
=SUM((TRIM(MID(SUBSTITUTE(A1,"+",REPT(" ",255)),1+(ROW(A1:A999)-1)*255,255)) & "0")/10)+1
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar:
Assuming you have your x+xxx+xxxxxxxxxxxx value in cell A1, consider the following formula below:
=SUM(0+TRIM(MID(SUBSTITUTE("+"&A1,"+",REPT(" ",99)),ROW(INDEX(A:A,1):INDEX(A:A,(1+LEN(A1)-LEN(SUBSTITUTE(A1,"+","")))))*99,99)))
When you return the formula, be sure to press CONTROL+SHIFT+ENTER. You can then just drag it down for your other additions.
You need to create a VB Macro:
Open the Tools>Macro>Visual Basic Editor
Right click to Create a new Module (important!)
Double click on the just created new module and paste this code
Public Function EvaluateString(strTextString As String)
Application.Volatile
EvaluateString = Evaluate(strTextString)
End Function
Close the VB editor window
Now you can use =EvaluateString(A1) to get the corresponding value of A1.
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 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 :)
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