+ Reply to Thread
Results 1 to 6 of 6

Issue converting string to number..

  1. #1
    Registered User
    Join Date
    08-04-2011
    Location
    Hillsboro, OR
    MS-Off Ver
    Excel 2003
    Posts
    14

    Issue converting string to number..

    I found a problem with my code where I was using CInt to convert string values into numeric. However, I have tried using CDbl and CLng and I still don't get the value. "27623" converts without issue, "72702" will not. Is there any way that makes sense?

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Issue converting string to number..

    I just ran the following without problem
    Please Login or Register  to view this content.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Issue converting string to number..

    The Integer data type is limited to the range -32768 to 32767.

    Mordred's conversion is (implicitly) CLng()
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    1,252

    Re: Issue converting string to number..

    With "72702", if you use CInt() or declare the variable as an Integer, then you will get an overflow error. This is because Integers in VBA are 16-bit and hold whole numbers between -32,768 and 32,767 (inclusive). Declaring the variable as a Long and using CLng() should be fine.
    Please Login or Register  to view this content.
    Hope that helps,

    Colin

    RAD Excel Blog

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Issue converting string to number..

    Is using the CLng() function a preferred way to parse a string over what I proposed?

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    1,252

    Re: Issue converting string to number..

    Yes, in VBA it is considered good practice to explicitly convert your data types rather than implicitly convert (coerce) your data types.

    As an aside: I know you were getting a bit into VB .Net on another thread. In VB .Net there's the statement "Option Strict On" which disallows narrowing.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1