lalebarde
lalebarde

Reputation: 1816

EXCEL VBA User Defined Function - why do I get the error "function not defined" while I use it in the Workbook where it is defined?

In the same Workbook, I have defined the function:

Public Function toto(val As Integer) As Integer
    toto = val * 2
End Function

Here is the editor view:

enter image description here

And in the Worksheet, in a cell, I use the formula:

enter image description here

But the result is (translation: #NAME?):

enter image description here

Also, in the VBA exec Window itself, opened from Excel, I get the following error when trying to execute:

toto(2)

enter image description here

Translation : Compilation error: Sub or Function not defined

What do I miss?

Upvotes: 0

Views: 1009

Answers (2)

Harvey Ellis
Harvey Ellis

Reputation: 606

Expanding on what @Jo.lass and @vincent-g said, you need to have your code in a module, not in worksheet or workbook code. See here:

enter image description here

Upvotes: 3

JLCH
JLCH

Reputation: 803

Instead of writing the function in a worksheet in VBE, try writing it in a new module. Works for me at least.

Upvotes: 2

Related Questions