I defined a few functions in a workbook using VBA, and then expected to be able to use them in a cell formula - but Excel does not recognise the function. I just get #NAME?<
I faced the same issue, after struggling around following worked for me:
My function was inside a module in macro workbook called Personal.XLSB. I prefixed the function name with the personal macro workbook filename and !, so if function name is theFunction(x,y) I entered in cell "=PERSONAL.XLSB!theFunction(x,y). This worked.
Please note that PERSONAL.XLSB is always open in hidden mode for me.
If you are using the latest versions of Excel, to see VBA functions in another workbook you need to:
Save your workbooks as .xlsm
Enable macros as suggested above
Set a reference. In VBA (Alt-F11) choose Tools/References, then browse to the workbook which contains the macro you want to use. Check that reference in the list.
If you get an error message about the module names clashing just rename it in the project explorer first.
Make sure you are not in design mode.
There is a design mode button on the developer tab in excel and beside the run/stop buttons in the VBA editor. If it is selected, and won't let you unselect it, then try reopening the workbook with macros enabled.
If it still is enabled, or won't let macros run, make sure macros are enabled.
Activate macros.
- Excel 2010
- Excel 2007
-- https://stackoverflow.com/a/20659823/258482
Putting the function in the "ThisWorkbook" area can cause the #NAME?
problem. Create a new Module (Right Click on the VBAProject Folder, Insert, New Module)
and put the function there instead.
Alt + F11
on Windows / Fn + Option + F11
on a Mac)Create a Public
function inside Module1
, for example:
Public Function findArea(ByVal width as Double, _
ByVal height as Double) As Double
' Return the area
findArea = width * height
End Function
Call it from a cell, like any other function: =findArea(B12,C12)
XLSX file and XLSM files have nothing to do with it. Format plays the role when you save the file. (In XLSX, VBA code will be stripped while saving the file).
The below code from http://office.microsoft.com/en-us/excel-help/creating-custom-functions-HA001111701.aspx works quite well inside a new module in my excel.
Function Discount(quantity, price)
If quantity >= 100 Then
Discount = quantity * price * 0.1
Else
Discount = 0
End If
Discount = Application.Round(Discount, 2)
End Function
Given that I cannot see your code, can you try whether below function works for you too? If so, start modifying the below function so that it becomes your function (for example, first change the name and see if it works, and then change number of parameters and check if it works, and then change name of parameters).
I opened Excel, opened the code editor (Alt+F11), selected the new workbook, inserted a new Module, typed
Function Decrement(i As Integer) As Integer
Decrement = i - 1
End Function
then went back to my workbook and in A1 typed =Decrement(2) and hit Enter, and it worked. Decrement showed up in the drop-down list of functions as I typed =Decr... It was recognized, and it worked. I didn't even have to save the workbook.
I know this isn't exactly an answer to your question, but it's the recipe I had luck with.