none
Problem with the simple execution of a new function RRS feed

  • Question

  • Hi everybody, I am trying to learn something about VBA and obviously I am starting from the very basics. However, I have already incurred in a problem. As you can see below, I wrote a simple function and there seems not be any problem in the writing. 

    Function findArea()
    Dim Height As Double
    Dim Width As Double

    Height = InputBox("Insert Height", "insert number")
    Width = InputBox("Insert Width", "insert number")
    findArea = Height * Width

    End Function

    However, I am unable to make execute it in Excel. I go to a cell, write =findArea() and it gives me the error  "#NAME?" as it does not recognize it. I have no idea on how to proceed...

    Thursday, June 13, 2019 3:25 PM

All replies

  • Make sure that the code for your function is in a standard module in the workbook where you use it.

    A standard module is the type of module that you create by selecting Insert > Module in the Visual Basic Editor.

    The function won't work as intended if it is in the module belonging to one of the worksheets or in the ThisWorkbook module.

    Nor will it work if it is stored in a module in another workbook. (There are exceptions to that statement, but they don't matter at the moment).


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, June 13, 2019 3:51 PM
  • By the way, I would prefer to write the function like this:

    Function findArea(Height As Double, Width As Double) As Double
        findArea = Height * Width
    End Function

    You can then enter the height and width in two cells, and refer to those in the formula:


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, June 13, 2019 3:54 PM