Pass function argument as formula - excel

Here is a code of my VBA script:
Function custom_if_formula(condition)
MsgBox(condition)
End Function
I am pasting the formula to any cell:
=custom_if_formula(B1="something")
The result in MsgBox is: TRUE or FALSE. Is it possible get in MsgBox as a result B1="something" as instead?
A pseudo-code of what I would like to achieve:
Function custom_if_formula(condition)
condition = condition.formula 'any method which take a literal string
MsgBox(condition)
End Function
P.S. My goal is to implement my own IFS function who behave identically like in Excel 2016. I am just curious if its possible. Tha'ts why I don't want to pass a string as an argument.

I think you're looking for:
Function custom_if_formula(condition)
If condition.Value = "something" Then MsgBox "something"
End Function
Where B1 is taken as the argument: =custom_if_formula(B1). Putting this in any cell (when B1 contains the string "something") will return:
You should really clarify what your intent is here, though. A UDF should return a value to its cell. Right now, it will just say 0 in the UDF's cell. Additionally, looking for "something" could be interpreted as looking for anything, and using this kind of verbiage leads to the "Who's on first, what's on second" ordeal...

Ok, I found a way how to do it:
Function custom_if_formula(condition)
cell_formula = Range(Application.Caller.Address).Formula 'I get an adress of a cell which call an UDF, and then take all string it contains
arg = Mid(cell_formula, 12, 100)
MsgBox(arg)
End Function

Related

Excel user defined function doesn't work properly when used in SUMIFS function

I have defined the function:
Function TabName()
TabName = ActiveSheet.Name
End Function
When I type =TabName() into a cell, I get the text tabname in the cell, as you can see here:
But when I use the function in a cell like this:
I get the wrong result. The correct result is what I get if I "hardcode" the tab name in like in this screenshot:
Why is this, and what can I do to make my function work properly?
You get the wrong result because your =SUMIFS() compares values to string value "=tabname()", not the result of function tabname(). Try this:
=SUMIFS(Bokningar!E:E;Bokningar!B:B;TabName())-SUM(C:C)

Application.Check spelling not returning correct Boolean value

I am trying to prepare a excel file which automatically shows if any word is misspelled. There are codes that shows and highlights the particular word field when word is misspelled ,however I want to highlight a different cell if any
of the words are misspelled.
This is what I did:-
Function GFD() As Boolean
Application.CheckSpelling (Range("a1").Text)
End Function
'=======================================================
Function GFF() As Boolean
Application.CheckSpelling (Range("a2").Text)
End Function
Problem is in the function I am not getting correct value, if I write "asdfsd" in a2 the function is set as False ,which is correct. But if I write "perfect" which is correct, it again returns False.
Please help.
You have to set the function to the value returned by the application:
Function GFD() As Boolean
GFD = Application.CheckSpelling(Range("a1").Text)
End Function

User defined function to get a value from a particular row with in the column of the calling function

I'm wondering if it's possible to write a user define function in VBA to get a value from a fixed row within the column of the calling function.
Ie I have some fixed values on row 2 of a Excel table; I want to refer to those values from a user defined function.
The caveat is I want the value in row 2, of the calling functions column.
Note : the actual function is more complex than this, however solving this problem would get me where I need to be.
Pseudocode:
Function GetStandardPayment()
GetStandardPayment = CallingColumn.Row(2).Value
End Function
What I have is the following:
Function GetStandardPayment()
GetStandardPayment = ActiveCell.Offset(ActiveCell.Column:2).Value
End Function
However it has syntax errors.
Edited to remove the possibly unneccessary variable.
You can use the Caller property of the Application object. When using a UDF in a worksheet cell, this will return a range object -- the cell where the function exists.
Function GetStandardPayment()
Application.Volatile
GetStandardPayment = Cells(2, Application.Caller.Column)
End Function
The Volatile property is used else the function won't update, since there are no cell references in the function argument.

select case with variable boolean operator

This is my first question on the forum, but reading previous questions has been enormously helpful in the project I'm working on, so already my thanks. I couldn't find an answer to this, but apologies if I overlooked something.
I am writing an excel macro in vba, and am trying to create a select case... statement in which the expression has a variable boolean and numeric component. For example, the macro can pull "> 3" or "< 3" from another worksheet.
My hope had been that I could assign to a string all of these parameters, i.e.:
test1 = "is " & BoolOperator1 & " " & NumericValue1
and then
Select case ValuetoCompare
Case test1
'Do something
Case test2
'...
Is there a way to do this? I suppose the alternative would be to nest the case with the numeric variable inside a select function that determines the operator, but I thought this would be more elegant.
Thanks in advance for your guidance--
Josh
Assuming that you'll get a string BoolOperator1 that is a valid operator, e.g. >=, =, and a numeric value NumericValue1, the easiest way to execute this comparison on another numeric value ValueToCompare is to use the Evaluate function. This will execute a string as VBA and return it's result.
In your case, you could simply use:
If Evaluate(ValueToCompare&BoolOperator1&NumericValue1) Then ...
If you want to use this in a Select Case statement, you'd either need to use a simple If ... ElseIf ... statement - or use this trick:
Select Case True
Case Evaluate(ValueToCompare&BoolOperator1&NumericValue1): ...
Case Evaluate(ValueToCompare&BoolOperator2&NumericValue2): ...
Case Else ...
End Select

Macro doesnt running Excel 2007 second parameter

I create a Public vba function with 2 parameters(module).
When I call the function I type "=InvoiceAmount2(A9;B9)".
The first parameter turns blue. The second black.
I remake the same function using one parameter, the second I use into the function, that way it´s ok. But I need two parameters
This is how you call a user defined function with two parameters:
=MyFunction(A1,B1)
Sample Code:
Function MyFunction(rCellA As Range, rCellB As Range)
MyFunction = rCellA.Value + rCellB.Value
End Function
I found the problem.
I call the udf function in formula constructor. I informed the parameters (A9 and B9)
finally, the udf function filled the cell. "=InvoiceAmount2(A9;B9)".

Resources