VBA why doesn't compare of text to number result in error?

TomCon

Active Member
Joined
Mar 31, 2011
Messages
373
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have this line in VBA
If (r.Offset(0, OfsPtile).Value < StdCut(i)) Then

The value on the sheet for r.Offset(0,OfsPtile).value is
--
(The character string "--" which is the result of an excel formula with IFERROR and "--" is the result when there is an error.

StdCut is dimensioned Double and has a value say .01

The above VBA statement evaluates to FALSE, not error. Why?

I am sure the value "--" is being compared in the VBA code as in the immediate window i type and get the following
?r.Offset(0, OfsPtile).Value
--

If i type the following in the immediate window, i get a VBA error
? "--" < .01

I want VBA to generate an error on the statement, not evaluate it as FALSE, just like it does in the immediate window

Is there an explanation for why the VBA IF statement evaluates as FALSE and not error?
Is there a workaround to use this IF statement and depend on it NOT evaluating to FALSE if it ends up comparing to a string?

Thank you.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You're asking IF "--" is less than .01, which is false. If I may guess at the rest, I'd say the lack of error is due to .01 being interpreted as text because the leading portion of the logical test is text. If you reverse it as .01 > "--" it might raise an error but I don't know. Some aspects of vba coerce data types when they are used - not sure why. If that test doesn't work as I imagine then it is likely that the presence of text coerces all values to text. That would be similar to how & handles Null differently than +. If you want to be sure to raise an error, then try a conversion function on the number, such as IF "--" < Cint(.01).
 
Upvote 0
The above VBA statement evaluates to FALSE, not error. Why?
Text is always > a number, I think it is possibly that it takes its ASCII number into account (just a guess). It doesn't evaluate to an error on a spreadsheet either.

Book1.xlsb
A
2aa
343
4
5
6TRUE
7TRUE
8FALSE
9FALSE
Sheet1
Cell Formulas
RangeFormula
A6A6=A2>A3
A7A7=A3<A2
A8A8=A3>A2
A9A9=A2<A3
 
Last edited:
Upvote 0
Not tested it but have you tried
VBA Code:
If r.Offset(0, OfsPtile).Value * 1 < StdCut(i) Then
 
Upvote 0
Solution
I want VBA to generate an error on the statement, not evaluate it as FALSE, just like it does in the immediate window
You are not comparing like with like.
If in the immediate window you put what your are using in VBA it will also return False.
eg ? "--" < .01 generates an error
but with the ActiveCell containing "--"
Rich (BB code):
? ActiveCell.Value
--
? ActiveCell.Value < .01
False

I think this is pretty close to what is happening:
Per: Microsoft > Comparison operators

IfThen
One Variant expression is numeric and the other is a stringThe numeric expression is less than the string expression.

Which in your case would mean "--" is greater than 0.01 hence your expression is returning False.

1669847939425.png
 
Upvote 0
Text is always > a number,
I don't think that is true. Textual comparisons are based on sort order so aaa sorts before aa so aaa>aa is true.

It doesn't seem to me to be related to Ascii values:
?Asc(43) : returns 52, which is Ascii for 4, not 43
?Asc(4)+Asc(3) : 103 . While that is greater than 97, it is not greater than 97+97 (aa)
even 1 is greater than a because numbers sort before letters.
?1>a
True
I suspect multiplying by 1 is just another means of converting to a number, same as Cint as suggested.
 
Upvote 0
I suspect multiplying by 1 is just another means of converting to a number, same as Cint as suggested
It is normally, or to produce an error if it can't be converted the same as you would do in normal cells although in a cell I would do
Book1.xlsb
A
2aa
399999999999
4
5
6#VALUE!
Sheet1
Cell Formulas
RangeFormula
A6A6=--A2>A3


The ASCII part was a guess as I have never seen MS explain it

so aaa sorts before aa so aaa>aa is true.
But aa is not a number. My statement was
Text is always > a number,
 
Last edited:
Upvote 0
PS:
I want VBA to generate an error on the statement, not evaluate it as FALSE, just like it does in the immediate window
You could force an error using cdbl(your_cell) but it would be much better to use If isnumeric(your_cell) = False to handle the issue
 
Upvote 0
Text is always > a number,
If you said "sorts before" I'd agree. Just using the word "number" isn't precise enough for this conversation IMO.
You can't compare text data type with number data type. Don't forget, 123 can be text data type as well as number data type. Without delimiting as "123" it will pass as number data type.
"aa" > .01 (text > number data type) will raise an error, which is why I suggested to convert.
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,655
Members
448,975
Latest member
sweeberry

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