Results 1 to 8 of 8
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    whats the function to convert string to numeric?

    Hi all
    VBA -

    I cant remember -
    Whats the function to convert a string to numeric type?
    TIA Diana

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the function to convert string to numeric?

    Diana

    It depends on what numeric format you want it converted to:

    CDbl
    CDec
    CInt
    CLng
    CSng

    If you want it validated before you convert, use:
    if isnumeric(string) ...
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the function to convert string to numeric?

    HI Geoff

    Convert a string into a numeric - integer type.
    Diana

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the function to convert string to numeric?

    Dina,

    In that case, use the CInt:

    Dim strInt As String
    Dim iOut As Integer
    If IsNumeric(strInt) Then
    iOut = CInt(strInt)
    End If

    This will fall over though for a large number outside of integer range (max 32767). "Long" is perhaps a better type to use (you would use CLng instead)- Integers can be dangerous for that reason.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the function to convert string to numeric?

    Geoff

    Thanks for that code - it worked.
    I wasnt aware of those functions ie 'CInt' etc -
    Learn something new every day!
    Thanks Diana

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: whats the function to convert string to numeric?

    Here's the one that got left out: Val(). It doesn't constrain the result to any particular data type, just converts it to a number.
    Charlotte

  7. #7
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: whats the function to convert string to numeric?

    Hey, that's a good one.

    Would that be useful to check if the number was in the range being converted to (ie, check if it was in a valid range, did not contain a decimal component? Or could I use "if typeof ..." to determine what sort of number conversion should take place? I tried the "is typeof" but couldn't find anything to work.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  8. #8
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: whats the function to convert string to numeric?

    TypeOf tells you the type of an object. You'd have to test the result of Val() using VarType() to get an idea of what kind of number it was. I have to warn you, though, that you'll need to wrap the result of Val() in and eval to get the right answer.

    VarType(eval(val("123456"))) returns 3, which is a long; but VarType(val("123456")) returns 5, which is a double. If VarType gets confused, it resorts to doubles to be sure it can hold everything.
    Charlotte

Posting Permissions

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