Consulting

Results 1 to 5 of 5

Thread: User Defined Function vba

  1. #1
    VBAX Regular
    Joined
    Oct 2018
    Posts
    43
    Location

    User Defined Function vba

    Hello Everyone,

    I am trying to define a Function on excel depending on the variable x.

    I wrote this code:

    Function F(x As Double) As Double



    Select Case x
    Case Is < 0: F = -Sin(x)
    Case 0 To 2: F = 2 * x
    Case 2 To 4: F = x ^ 2
    Case Else: F = 16
    End Select
    End Function

    But the only results I get are those ones:
    Capture.PNG

    Would you have some idea on what I did wrong?

    Thanks in advance!

    Edmond

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Works for me

    Make sure the function is in a Standard Module which is not Option Private Module, and that the function is not Private

    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Be sure to check the limits.
    Function F(x As Double) As Double  
      Select Case True
        Case x < 0: F = -Sin(x)
        Case x >= 0 And x <= 2: F = 2 * x
        Case x > 2 And x <= 4: F = x ^ 2
        Case Else: F = 16
      End Select
    End Function

  4. #4
    VBAX Regular
    Joined
    Oct 2018
    Posts
    43
    Location
    Thank you both for your answer.

    Paul, I just googled "Option Private Module" but I didn't find any explanation how to activate it or not. I mean without using code.
    Could you explain how you did to make it works?

    Thank you!

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    To use a UDF as a WS function, you do NOT want "Option Private Module" and you do NOT want "Private Function F()…"

    https://docs.microsoft.com/en-us/off...vate-statement

    It's a statement in the module, and not really controlled by any user code


    Since you were getting #NAME errors, my GUESS was that you might not have F() in a Standard Module, like my screen shot and attachment

    If that's not it, attach the troublesome workbook
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •