Why is text >5

dee101

Active Member
Joined
Aug 21, 2004
Messages
261
Why does this macro show the message box when text is put in cell A1
I did not think text is >5, do you have to check to see if A1 is a number first? If so how would you do it?
Thanks

Sub Macro1()
If Range("A1") > 5 Then
MsgBox "Greater than 5"
End If
End Sub
 

Oaktree

MrExcel MVP
Joined
Jun 20, 2002
Messages
7,937
Try:

If IsNumeric(Range("A1")) And Range("A1") > 5 Then MsgBox "Greater than 5"
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,135
Office Version
365
Platform
Windows
I find this very interesting...

Does anyone have a technical explanation why Excel interprets text to be greater than 5?

Just trying to learn a little more how Excel thinks...
 

juster21

Well-known Member
Joined
Jun 3, 2005
Messages
867
jmiskey said:
I find this very interesting...

Does anyone have a technical explanation why Excel interprets text to be greater than 5?

Just trying to learn a little more how Excel thinks...
The low-tech answer is Excel interprets the text as a number b/c of the math operator ">". Somewhere deep in the belly of Excel it makes the link between "A1" and ">5" and makes that numeric automatically. Sorry, but that's the best I can explain it.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,135
Office Version
365
Platform
Windows
The low-tech answer is Excel interprets the text as a number b/c of the math operator ">".
I don't think that is it. In playing around, it appears that in the grand scheme of things, Excel "orders" text after numbers.

For example, if you pick a column in Excel, and enter various numeric entries, and various text entries, and then sort it, you will see that all the numeric entries appear before the text entries.

So I guess it is just Excel's order of operation.
 

RalphA

Well-known Member
Joined
May 14, 2003
Messages
3,829
Starting from ASCII character 0 (the NULL character), all the way to ASCII character 127, Excel sorts all the characters from character 0 to charaxxter 31, and character 127, first. Then, it sorts all the punctuation marks and other non-numeric and non-alphabetic charaxters, then, all the numerics (the ten digits, 0, 1, 2...9, and, finally, the 26 upper-case and the 26 lower-case letters of the alphabet. The letters are sorted in a non-case sensitive mode. For instance, if you sort A,B,a,c, you will get A,a,B,b. And, if you sort a,A,b,B, you will get a,A,b,B.

In closing, I don't know why Microsoft chose to go against the conventional method of sorting, stricktly in accordance with the ASCII code, and it was painful for me to find out this fact, through the help of others. So, I want to share this knowledge here. And, if anybody is really interested in doing the actual sort, send me a PM, include your email address, and I will get in touch with you on this.
 

Forum statistics

Threads
1,077,866
Messages
5,336,857
Members
399,109
Latest member
gdcuk

Some videos you may like

This Week's Hot Topics

Top