+ Reply to Thread
Results 1 to 4 of 4

Error using Clng function in vba to convert string to long data type

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Error using Clng function in vba to convert string to long data type

    I have this string for example

    Please Login or Register  to view this content.
    when i run this i get a run time error for type mismatch

    any ideas? is it because the string is empty?
    Last edited by welchs101; 06-05-2011 at 06:13 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Error using Clng function in vba to convert string to long data type

    Hi welchs101,

    The short answer is "you can't do that". You are trying to change a string into a number. You should get the same answer if you wanted to know the numeric value of "Bob". You have a Type of Variable mismatch.

    The VBA editor or runtime has no idea how to deal with this and gives you back an error.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Error using Clng function in vba to convert string to long data type

    Use Val, it is a super robust conversion function

    Val("") = 0
    Val("smith") = 0
    Val("123cat") = 123
    Val("3.14159")=3.14259
    Val("-23.8") = -23.8
    Val("5/23/2001") = 5

    If you definatly need a data type long

    use CLng(Val(someString)) to buffer against errors.
    Last edited by mikerickson; 06-04-2011 at 11:19 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Error using Clng function in vba to convert string to long data type

    thanks for the info guys! very helpful.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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