... 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.
 
Okay, followup question then. Findme is going to = 2, but ultimately FinalFind is going to be a cell value. How would I compare the two?
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Okay, followup question then. Findme is going to = 2, but ultimately FinalFind is going to be a cell value. How would I compare the two?
Assuming you mean you will assign the cell value to the FinalFind variable, you can do it the same way you do it now (if you declare FinalFind as a numeric data type, VB will coerce any value, text or number, to that data type on assignment).
 
Upvote 0
Gotcha, leave the declarations out of this one.
Actually, no. It is almost always best to explicitly declare your variables, so they behave like you want them to.
You just need to be sure that you declare them properly.
 
Upvote 0
Actually, no. It is almost always best to explicitly declare your variables, so they behave like you want them to.
I complete agree with Joe on this... declaring your variable using the correct data type helps avoid unintended results. For example, let's say you have two undeclared variable, var1 and var2, and two Forms TextBoxes on the worksheet that a user types numbers into. So you assign the text in TextBox1 to var1 and the text in TextBox2 to var2 and, later on, you want to display their sum, so you write...

Code:
var1 = ActiveSheet.Shapes("TextBox 1").TextFrame2.TextRange.Text
var2 = ActiveSheet.Shapes("TextBox 1").TextFrame2.TextRange.Text
MsgBox var1 + var2

You will probably be surprised at the answer you get... it will be the concatenation of the two values, not their sum. Now, if you remember to do so, you can wrap each assignment with a CLng or CDbl function call to force the coercion of the values to numeric values OR you can properly declare var1 and var2 to be Long or Double and let VBA handle that for you.
 
Upvote 0
I just meant leaving that one variable undeclared on this one thing, but I get what you're saying.
 
Upvote 0
I just meant leaving that one variable undeclared on this one thing, but I get what you're saying.
Based on your comment, I am not sure that you do get what we are saying.
Why do you think you should leave it undeclared on this one thing? I cannot think of a single good reason to want to do that.

Since you want it to behave like a number, you should declare it as a number, i.e.
Code:
Dim FindMe as Long
Dim FinalFind as Long
or
Code:
Dim FindMe as Double
Dim FinalFind as Double
It doesn't matter if the value is being hard-coded, or passed from a cell value, it should still be declared.
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,677
Members
449,092
Latest member
tayo4dgacorbanget

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
Back
Top