0

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?

2
  • 2
    Where did you put the code? It need to be in a public module. Commented Mar 7, 2019 at 14:55
  • FWIW the Val parameter is shadowing the VBA.Conversion.Val function and should probably be renamed. Commented Mar 7, 2019 at 15:16

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Have you tried just putting it in a module by itself? Also have you called the module the same thing? The other instance where you can get the #NAME error is when there is a name conflict.
@lalebarde no, that's not what you have. Per your screenshot, your code is in the code-behind of Feuil5, not in a standard module.
Sorry, I missed the left part "Module 1". Now it works !
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.