+ Reply to Thread
Results 1 to 4 of 4

Weird string to int conversion

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2013
    Location
    Belgrade
    MS-Off Ver
    Excel 2003
    Posts
    1

    Weird string to int conversion

    Hello everyone.

    This isn't really a problem, cause I solved it easily by explicit int conversion. But something weird happens in this code which diverges from my understanding of VB workings, and I'm trying to find out what's going on . Wondering if some expert could explain.
    If GameNo < FirstGame Or GameNo > LastGame Then
        MsgBox "Game number " & GameNo & " not present in the sheet."
        Exit Sub
    End If
    GameNo contains "4830" (string), and LastGame contains 4830. Yet second part of if condition yields True. LastGame/FirstGame/GameNo aren't declared with Dim, and when LastGame is declared as Dim LastGame As Integer everything works as expected.
    So I was wondering if this is some bug, or there are some additional rules with converting from/to variant types?

    See this image for more details:
    vba-weird-situation.png

    Thank you very much!

    P.S. It's Excel 2003

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Weird string to int conversion

    Sorry misread.


    I could not duplicate problem.
    Last edited by Solus Rankin; 07-23-2013 at 11:48 AM. Reason: misread
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,616

    Re: Weird string to int conversion

    I'"m no VBA man, but if you enter ="4830">4830 in a cell it yields TRUE because XL first evaluates the TYPE() function. ( before any other comparison)
    TYPE(string) =2 and TYPE( number) =1
    If you enter ="1">4830 the answer will be TRUE also

    Has that anything to do with your question?

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Weird string to int conversion

    I think because you have two variants no implicit type coercion occurs but I cannot recall seeing that documented (although the Let coercion rules for Variants in the Runtime semantics section of the VBA language spec would imply no coercion should occur, which makes sense since neither variable is of a more specific type and therefore there can be no indication as to whether the comparison is intended to be on a string or numeric basis)

    edit: reading further into the spec, it does explicitly state (below table 5.61)
    There is an exception to the rules in the preceding table when both operands have a declared
    type of Variant, with one operand originally having a value type of String, and the other
    operand originally having a numeric value type. In this case, the numeric operand is considered
    to be less than (and not equal to) the String operand, regardless of their values.
    Last edited by JosephP; 07-23-2013 at 12:36 PM.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. String conversion formula?
    By JGTExcel in forum Excel General
    Replies: 3
    Last Post: 02-10-2012, 01:12 PM
  2. String to number conversion, not simple (for me)
    By PaulLag in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2011, 04:49 AM
  3. String to Date Conversion
    By dasreinfeld in forum Excel General
    Replies: 10
    Last Post: 11-18-2010, 07:15 PM
  4. InputBox String Conversion
    By Paul987 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-20-2007, 03:51 PM
  5. Simple String Conversion - HELP
    By prepotency in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-10-2005, 10:03 AM

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