Results 1 to 9 of 9

Thread: Val function and percent symbol

  1. #1
    larryn
    Guest

    Val function and percent symbol

    Example:

    Val("1.6%") returns a type mismatch error. Why?

    the decimal is valid within Val()

    shouldn't this return 1.6 ?

    anyone?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Lose the % sign.

  3. #3
    larryn
    Guest

    the whole purpose of Val () is

    to lose the percent sign for me....

    anyone....except hack?

  4. #4
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Why are you using the Val() function anyway? The value of 1.6 is 1.6 =p
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Use the tag propery to store the number. Then use val on that number, not the one displayed to the user...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6
    larryn
    Guest
    Hack, you are correct to a degree, but you left
    out one important point - which is this:

    The Val () function is designed to return the numbers
    contained in a string as a numeric value of appropriate type.

    When it reaches a non-numeric it is supposed to stop
    reading the string.

    So if i have a string value such as "1.6%" in an excel
    spreadsheet cell, and extract that data minus the %
    sign, i should be able to use Val () to do it...

    Anyway.....

    I wrote my own Val () function which I think
    is better than the original...

    here it is:

    Code:
    Public Function Val2(sString As String) As Double
    Dim sTemp As String
    Dim sOut As String
    Dim iX As Long
    Const VALID = "1234567890."
    
    For iX = 1 To Len(sString)
        sTemp = Mid$(sString, iX, 1)
        If InStr(VALID, sTemp) <> 0 Then
            sOut = sOut & sTemp
        End If
    Next
     
    Val2 = sOut
    End Function

  7. #7
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Originally posted by larryn
    Anyway.....

    I wrote my own Val () function which I think
    is better than the original...
    too bad it doesn't work on "-123.456"


    -Lou

  8. #8
    larryn
    Guest
    just put a negative sign into the VALID constant..

    "1234567890-."

    that wasn't so hard was it...?

  9. #9
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    If you add the " . - ", this will let me type multiple of these keys and thus create a non-numeric entry correct?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

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