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.
 
Thanks for all the discussion it was interesting. I can attain getting an Error by either of two methods about (at least, maybe more). But these two are simple and appealed to me. Either multiply by 1 or convert with cdbl() or cint(). Never know which to mark "check" when more than one solve the problem. They should let you mark more than 1 as "check".

I noticed another difference within code, it must have to do with conversion rules being different in different cases, but it seems inconsistent and a big confusing to me.

If your VBA code has
v="--"
IF(v<.01)...
It will evaluate to FALSE and the code will proceed.

But if you have

IF("--"<.01)...
this will give an error.

Is it that assignment converts to a Variant and that is friendly to comparison but the string itself is not?
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
If you don't set a data type for v it will default to a variant, and the rule in my post #5 will apply.
If you Dim v as String it will error out in the same way as using "--" directly.

As per my post #9 I would recommend handling it with a check for IsNumeric.
 
Upvote 0

Forum statistics

Threads
1,215,660
Messages
6,126,085
Members
449,287
Latest member
lulu840

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