I am moving a excel VBA from PC to Mac and one line has an Evaluate function, for example:
Public Function test()
test = Evaluate("=2+2")
End Function
However, if I try and run the macro on the mac the function returns #name. Is there an equivalent function on the Mac side to Evaluate?
There is nothing wrong with the above function.
If you are calling the above function in VBA then it works as expected and If you are using it as a UDF i.e you are calling it from the worksheet then #Name error means that you have not pasted the function in a module. Move the function from the Sheet/Workbook code area into a module and then try again.
Related
I have created a UDF Excel via ALT+F11 in own Module.
Have read many tutorials where everything seems so easy (that you can use your UDF in each Cell of opened Workbook). But I can't use my function.
Tried two test functions in Module (Test and Test2).
Checked everything (disabled all Macro security, allow access to VBA project model) but I don't see my custom function when I type "=" in a cell.
I'm using Excel in active Office 365 license so it is the newest version.
What could cause that my UDF is not applicable in the Excel cell?
I just get "#NAME?" / "#VALUE?" as result and no auto suggestion of function name...
maybe anyone has a hint for me what could cause that issue. Thx!
Public Function test()
test = 1
End Function
Public Static Function test2()
test2 = 123
End Function
#RonRosenfeld,
This is to demonstrate what behaviour I'm seeing:
Scenario 1: Function pasted in ThisWorkbook calling it in Sheet1:
Scenario 2: Function pasted in Sheet2 calling it in Sheet1:
Scenario 3: Function pasted in Module1 calling it in Sheet1:
This is why I came to the conclusing the () must be missing to show the #NAME error since otherwise there would be a syntax error.
#OP, You seem to have put your function in a class module, instead of a regular module.
Functions located in a module created using: add a Module(second menu from left)/Module
succesfully shows up in the function menu.
I am trying to write a function in VBA, that I can then use in excel...
This is not the exact function that I want to create, but I'm having a lot of trouble so I simplified it down a lot...
Suppose I have the following matrix in my excel spreadsheet.
I know there is a built-in function in excel to find the determinate. Now, I want to use that in my function I define in VBA. Again there will be more stuff in my function, but I'm just trying to understand what I'm doing wrong.
So suppose this is my function I've defined in VBA...
Option Explicit
Public Function determinate(mtx As Range)
determinate = Application.MDeterm(mtx)
End Function
However, when trying to use determinate() in excel it doesn't calculate. Why is this/How can I fix this?
I'm using this VBA macro in an Excel 2010 workbook to define the function FileSize, which enables me to pull the size of a document into a document master worksheet using the filepath.
Function FileSize(FileName As String)
FileSize = FileLen(FileName)
End Function
I then use the FileSize function to reference a file path string in column A like so:
=FileSize(A1)
This works in the workbook I wrote it for initially, but when I copypaste the macro for the Function into a new module for a new worksheet, I get an invalid name error.
Both workbooks are macro-enabled (.xlsm), and activating or deactivating option explicit hasn't had any effect. What am I doing wrong?/What am I neglecting to do?
#NAME suggests that your new workbook has no idea what the formula =FileSize() is referring to.
Double check to insure that you are actually putting that function in a module in the same workbook in which you are using the formula.
Make sure that you have defined the code as a Function and not a Sub.
Rip your computer off your desk and throw it out the window.
Thank you, JNevill! Luckily, defenestration won't be necessary.
I figured it out--I gave my module a name that was identical to the function it contained. Just changed the module name, and now the functions populate correctly.
I want to write a little logging function in an excel add-in that I will be calling from many different workbooks. I'd like to be able to just call it by passing only the log text, and the log function itself could handle the timestamp, workbookname, etc.
However, I cannot use either ThisWorkbook or ActiveWorkbook to determine which workbook was responsible for making the call, as Thisworkbook will return a reference to the add-in itself, whereas VBA code running in a workbook other than the workbook with active focus in Excel could make the call, but the ActiveWorkbook will return the one that has focus in the window.
Application.Caller looked like a possible solution, but this seems to work only when the function is called from a cell, not from VBA.
Is what I'm trying to do impossible?
Update
According to > 1 person, this is in fact impossible. If anyone happens to know some clever workaround please speak up.
Ok, so having read the question properly I'll try again...
So, to state the problem:
you want a routine written in an addin, that when called from vba in another workbook can work out (among other things) which workbook contains the vba that made the call, without having to pass this information explicitly.
As stated this is not possible (this is a similar question to accessing the call stack from code: something that is to my knowledge not possible)
However you can almost get what you want like this
Declare your log function like this:
Sub MyLogger(wb as Workbook, LogText as String)
Dim CallerName as String
CallerName = wb.name
' your code...
End Sub
Then wherever you call the sub use
MyLogger ThisWorkbook, "Log Text"
Not quite as good as passing nothing, but at least its always the same
To get the name of the calling workbook, use
Application.Caller.Worksheet.Parent.Name
Application.Caller returns information about how Visual Basic was called. If called from a custom function entered in a single cell, a Range object specifying that cell is returned
Having got a reference to the cell, .Worksheet.Parent.Name gives you the name of the workbook
Note that Application.Caller will return other things depending on how your function is called (see VBA help for details)
In an Add-In Function called by an Excel Worksheet Array Entered Function Call, I find that "Application.Caller.Parent.name" gives the Sheet Name (Tab Name, not sheet number).
I had the same issue when coding a custom function. Function works well, but anytime another workbook is calculated or activated, all cells using that function revert to #value. It can be very frustrating when working with multiple files using this formula.
To get the Workbook I used:
Dim CallingWb As Workbook
Set CallingWb = Application.Caller.Parent.Parent
This should work if your function is in a cell.
Too late for the original post, but might help others!
I want to write a Excel function like this. It is an extension of the Dec2Bin function
Public Function Dec2BinEx(x As Long)
Dec2BinEx = dec2bin(x) + 10
End Function
But I am getting an error when trying to use it. How do I call a excel function inside the visual basic editor
In general, you call Excel functions with Application.WorksheetFunction.SomeFunctionName. However, Dec2Bin is special, as it is an Add-In function, and not a pure Excel function. Hence, Application.WorksheetFunction does not work here. Instead, you have to make the functions of the add-in available to your code. To do so, follow these steps
In Excel, menu Tools/Add-Ins, make
sure that the add-in Analysis
ToolPak - VBA is imported.
Then, set a reference to this add-in
in your code: in the VBA editor, menu
Tools/References, add a reference to
atpvbaen.xls.
Then, your original code, as posted in your quesiton should work just fine.
You will first of all have to create a module eg from menu select Insert->Module. Then inside this module create a function named main. This function is run by default when code is run. Now inside this function call your own function like this:
Sub main()
Call Dec2BinEx(your_value_here)
End Sub
Public Function Dec2BinEx(x As Long)
Dec2BinEx = dec2bin(x) + 10
End Function
Having done that, make sure that you have the reference to dec2bin function or if you create that too. Thanks