Thanks Thanks:  0
Likes Likes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: ... Why is Excel saying 2 > 12?

  1. #1
    Board Regular Technowolf's Avatar
    Join Date
    Aug 2014
    Posts
    180
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default ... Why is Excel saying 2 > 12?

    Okay. I don't know if I'm just fried or if Excel is having an stroke or what, but here's my code:

    Code:
        FindMe = 2
        FinalFind = 12
    FinderLoop:
        If FindMe < FinalFind Then
            FindMe = FindMe + 1
            GoTo FinderLoop
        End If
    It's skipping over my If statement, in other words Excel is saying 2 > 12. In fact, if I change it to If FindMe > FinalFind, then it goes through.
    Please tell me I'm just overlooking something stupid.

  2. #2
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    47,922
    Post Thanks / Like
    Mentioned
    40 Post(s)
    Tagged
    8 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    It works fine for me.
    Have you declared the FindMe and FinalFind variables in your code?
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  3. #3
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    33,903
    Post Thanks / Like
    Mentioned
    71 Post(s)
    Tagged
    28 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    Quote Originally Posted by Technowolf View Post
    Okay. I don't know if I'm just fried or if Excel is having an stroke or what, but here's my code:

    Code:
        FindMe = 2
        FinalFind = 12
    FinderLoop:
        If FindMe < FinalFind Then
            FindMe = FindMe + 1
            GoTo FinderLoop
        End If
    It's skipping over my If statement, in other words Excel is saying 2 > 12. In fact, if I change it to If FindMe > FinalFind, then it goes through.
    Your code works for me... the only way I can get it to do what you say it is doing for you is to Dim FindMe and FinalFind as String variables OR, if those variables are Variants, assign text values to them.
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  4. #4
    Board Regular Technowolf's Avatar
    Join Date
    Aug 2014
    Posts
    180
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    Yup.

    Code:
    Dim FindMe As String, FinalFind As String

  5. #5
    MrExcel MVP Zack Barresse's Avatar
    Join Date
    Dec 2003
    Location
    Oregon, USA
    Posts
    10,724
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    That was my first guess, they're strings, in which case it would make sense. You'd need to post the entire code for us to get anywhere though.

    Edit: Ah, just saw your reply. Declare them as Long, if they're going to be whole numbers anyway.
    Regards,
    Zack Barresse
    My Book on Excel Tables
    (If you would like comments in any code, please say so.)

  6. #6
    Board Regular Technowolf's Avatar
    Join Date
    Aug 2014
    Posts
    180
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    So I shouldn't declare them as String? How come?

  7. #7
    MrExcel MVP
    Moderator
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    47,922
    Post Thanks / Like
    Mentioned
    40 Post(s)
    Tagged
    8 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    Dim FindMe As String, FinalFind As String
    So I shouldn't declare them as String? How come?
    Numerically speaking, 2 comes before 12, but alphabetically speaking "12" comes before "2" since "1" comes before "2".
    If you want them to behave like numbers, then you need to declare them as numbers.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  8. #8
    Board Regular chicagocomputerclasses's Avatar
    Join Date
    Mar 2015
    Location
    Chicago, IL
    Posts
    645
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    Joe4 is spot on, it must be interpreting one of those variables as string.

  9. #9
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    33,903
    Post Thanks / Like
    Mentioned
    71 Post(s)
    Tagged
    28 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    Quote Originally Posted by Technowolf View Post
    So I shouldn't declare them as String? How come?
    Because you appear to be assigning numeric values to them, so declare them as a numeric data type (Long if they will always be whole numbers, Double if they will be floating point numbers).
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  10. #10
    MrExcel MVP Zack Barresse's Avatar
    Join Date
    Dec 2003
    Location
    Oregon, USA
    Posts
    10,724
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: ... Why is Excel saying 2 > 12?

    No, you shouldn't declare them as String. A String is a textual variable. It will try to make everything a string. Think of it as enclosing it in quotes. In fact if you look at the variable at run-time in the Locals window, it will be enclosed in quotes. To be able to evaluate it as a number, it must be declared as a number type, such as Long, Integer, Single, or Double (the latter two being types to handle decimals).
    Regards,
    Zack Barresse
    My Book on Excel Tables
    (If you would like comments in any code, please say so.)

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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