Macro doesnt running Excel 2007 second parameter - excel

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)".

Related

"Wrong Data Type" error for setting cell value [duplicate]

I am building a User Defined Function.
I get an error
A Value used in the formula is of the wrong data type
I am trying to build a function that adding one comment, will also add the comment to another location as the comments always come in pairs.
Public Function AddComments(vesselCell As Variant, shopCell As Variant, comment As String) As Variant
Range(vesselCell).AddComment (comment)
Worksheets("Shop").Range(shopCell).Value = comment
End Function
I have singled it out to the third line of code causing the problem.
The setup currently is the Vessel Cell will be a comment added to the sheet, and then the Shop sheet has a section for comments as a column.
Assuming vesselCell and shopCell are both Range objects, Range(vesselCell) and Worksheets("Shop").Range(shopCell) are part of the problem.
Make them Range, not Variant.
vesselCell.AddComment comment
shopCell.Value = comment
Now, this code isn't legal in a UDF. Make your procedure a Sub procedure (remove the As Variant return type), and invoke it from other VBA code, or attach it to a shape or button's OnAction property.
User-Defined Functions take inputs, compute a result, and then return that result to the calling cell: your code not returning anything is a strong indicator that a Function isn't appropriate here.

Pass function argument as formula

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

Simple custom function in Excel, using Vlookup

I want to have a faster process looking up cross references.
Right now I use VLOOKUP, and it works fine - but it takes time when it needs to be done multiple times everyday.
It is always the same sheet I use to lookup the cross references, so the only thing that changes is my input value in the VLOOKUP function.
Therefore I want a function where I only input 1 value to get the VLOOKUP value.
The idea is a function like:
=CROSS(ID)
where
CROSS = vlookup(ID, table_array, col_index_num,[range_lookup])
So the vlookup_value is replaced by ID.
I hope you can provide me with some answers - thanks in advance.
I have tried multiple different things, but with no success.
As I am new, I've googled and recorded macros to look for answers.
You could write a UDF (user defined function) for that, using the WorksheetFunction.VLookup method:
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, table_array, col_index_num, range_lookup)
End Function
I got it working as it should now!
The code ended up like this:
Sub crossref()
Option Explicit
Public Function CROSS(ID As Variant) As Variant
CROSS = Application.WorksheetFunction.VLookup(ID, Worksheets("Sheet1").Range("E:F"), 2, 0)
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.

Expected array when calling function

I'm trying to learn a little VBA an I'm in the process of saving ranges as CSV files.
However, I'm having trouble with this line of code
Call SaveRangeAsCSV(SelectDown("B5:D5"), "C:\Test\test.csv", True)
And here is a snippet of the SelectDown function.
Private Function SelectDown(Range As String) As Range
SelectDown = Range(Range, ActiveCell.End(xlDown)).Select
End Function
I get the error: Expected array. I cannot seem to understand what the problem is. Any help would be appreciated :-)
It sounds like the function SaveRangeAsCSV is expecting an array, but you are passing in a Range. This function is not a built-in Excel function, so I can't check it. Maybe you could check what arguments it is expecting?
My function now looks like this and is working perfectly.
Private Function SelectDown(RangeAddress As String) As Range
Set SelectDown = Range(RangeAddress, ActiveCell.End(xlDown))
End Function

Resources