Results 1 to 6 of 6

Thread: Public Function VBA #VALUE Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Public Function VBA #VALUE Error

    Hi,

    I have created a simple Public Function (to be expanded) but want to make sure I can get the call in excel working. Here is the code:

    Public Function Wind (Direction as integer) as integer

    If Direction > 0 & Direction <=10 Then Wind = 1
    Else: Wind = 0
    End If

    End Public Function

    Essentially Direction is a numeric value for wind direction and i am seeing if it is between 0 and 10. When i type the following in excel
    =Wind (A1) it returns #VALUE
    Any help would be appreciated

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Public Function VBA #VALUE Error

    Thread moved from the 'VB6' forum to the 'Office Development/VBA' forum (while VBA and VB6 have some similarities, they are not the same thing)

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Public Function VBA #VALUE Error

    You are in the right track. A couple of changes:
    * & is concatenation for string, use "And" instead.
    * End Function, not End Public Function
    * note: This function does not include the "0", if you want it do >=0
    * If you put a statement right after the Then keyword, you cant put Else on the next row.

    One cosmetic change:
    * Structure your code for readability, saving code rows making it compact at the expense of readability is not a good practice.

    vb Code:
    1. Public Function Wind(Direction As Integer) As Integer
    2.  
    3.     If Direction > 0 And Direction <= 10 Then
    4.         Wind = 1
    5.     Else
    6.         Wind = 0
    7.     End If
    8.  
    9. End Function
    Last edited by kaliman79912; Aug 5th, 2011 at 12:37 PM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Re: Public Function VBA #VALUE Error

    Thanks for the reply, it worked great. As a follow-up I wanted to see if it would be possible to only use the below code for a wind speed greater than 15km/hr. I added the Speed in the function but do not know how to approach only running the code for Speed > 15

    Public Function Wind(Direction As Integer, Speed as integer) As Integer

    If Direction > 0 And Direction <= 10 Then
    Wind = 1
    Else
    Wind = 0
    End If

    End Function

  5. #5
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Public Function VBA #VALUE Error

    When posting code try to format it as such, with the code tags at the top of the edit window.

    vb Code:
    1. Public Function Wind(Direction As Integer, Speed as Integer) As Integer
    2.  
    3.     If Direction > 0 And Direction <= 10 And Speed > 15 Then
    4.         Wind = 1
    5.     Else
    6.         Wind = 0
    7.     End If
    8.  
    9. End Function
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Public Function VBA #VALUE Error

    7wgh, Few suggestions

    When you are creating a Public Function you need to take care of many things.

    Use a variable name which is not a part of the Excel Object Model. For example "Direction". it is used by

    Speech.Direction Property

    So how does one know if the variable name is already a part of the EOM. It's simple
    The moment you type the variable name and move to the next line, the first letter will automatically get capitalized unless you have previously defined it that way.

    Also in your code, you have to take care that if the wrong values are passed in the function than it should not give you unexpected results

    I would suggest this code in lieu of your original code. Let me know what do you think

    Code:
    Public Function WindValue(RngDir As Range, SpeedRng As Range) As Long
        On Error GoTo Whoa
        
        '~~> Check if the values passed are numeric
        If IsNumeric(RngDir.Value) And IsNumeric(SpeedRng.Value) Then
            Dim lngDirection As Long, lngSpeed As Long
            
            lngDirection = RngDir.Value: lngSpeed = SpeedRng.Value
            
            If lngDirection > 0 And lngDirection < 11 And lngSpeed > 15 Then
                WindValue = 1
            Else
                WindValue = 0
            End If
        Else
            WindValue = 0
        End If
        
        Exit Function
    Whoa:
        WindValue = 0
    End Function
    Hope this helps.

    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width