... Why is Excel saying 2 > 12?

Technowolf

Board Regular
Joined
Aug 28, 2014
Messages
181
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.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,912
Office Version
  1. 365
Platform
  1. Windows
It works fine for me.
Have you declared the FindMe and FinalFind variables in your code?
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,340
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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.
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web

ADVERTISEMENT

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.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
58,912
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,340
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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).
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
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).
 

Forum statistics

Threads
1,140,929
Messages
5,703,225
Members
421,285
Latest member
Bebek

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top