How to call VBA function from Excel cells (2010)?

佐手、 提交于 2019-12-17 07:27:41

问题


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?

Tried:

  • Realising I had created an XSLX file, I converted it to a XSLM file. Didn't work.
  • Removed all types from the function declaration. Didn't work.
  • Moved the function into the worksheet VBA module. Didn't work.
  • Added Public to the declaration. Didn't work.

What am I missing?

This isn't clever code, either:

Function Square2(AnyNumber)

'return the square of any integer
Square2 = AnyNumber * AnyNumber

End Function

回答1:


Answer

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.

Steps

  1. Open the VBA Editor (Alt + F11 on Windows / Fn + Option + F11 on a Mac)
  2. Right-click VBAProject
  3. Select Insert >> Module
  4. 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
    
  5. Call it from a cell, like any other function: =findArea(B12,C12)




回答2:


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.




回答3:


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




回答4:


I had an identical problem, including a working function that later stopped working giving a #NAME error. I have managed to fix both by making sure the name of the module is not the same as the name of the function. I had a working function F_1 in Module1, I changed the module name to F_1 and it stopped working, now back at Module1 and the function works again. My second function also began working when I changed the module name from F_2 to Module2.




回答5:


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




回答6:


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.




回答7:


I think there might be a problem if your Module has the same name as your function. Try renaming your module or your function.




回答8:


Activate macros.

  • Excel 2010
  • Excel 2007



回答9:


If you are using the latest versions of Excel, to see VBA functions in another workbook you need to:

  1. Save your workbooks as .xlsm
  2. Enable macros as suggested above
  3. 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.



来源:https://stackoverflow.com/questions/12351339/how-to-call-vba-function-from-excel-cells-2010

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!