Re: Another VBA bug
- From: "Peter T" <peter_t@discussions>
- Date: Mon, 14 Jan 2008 21:02:55 -0000
OK we agree that
cDbl("($1,23,,3.4,,,5,,E67$)") ' change '$' to local currency symbol
returns a value and also the string will coerce to a number if assigned to a
variable declared as a double.
Therefore surely we should expect IsNumeric to return true when testing that
string, which it does however surprising at first it may seem. So I'm
wondering after all that, is there anything actually unreliable or buggy
about IsNumeric.
However I still suspect Help might be better written along the lines I
suggested using the word 'coerced' vs 'evaluated', as further suggested in
the following
Sub test()
Dim s$, v
Dim dbl As Double
Dim dt As Date
s = "3,4.5,,,6,7"
s = "3.45"
v = Application.Evaluate(s) ' Error 2015
dbl = s ' 34.567
Debug.Print v, dbl, IsNumeric(s)
' can coerce but can't evaluate
''' Date
Debug.Print IsNumeric(Date) ' false as per help
s$ = Format(Date, "mmm dd yyyy")
dt = s ' OK
dbl = CDate(s) ' OK'
dbl = s ' error
'can only coerce to variable declared 'As Date'
End Sub
btw, behind the scenes is there any difference between doing
dbl = s vs dbl = cDbl(s) ?
I guess for consistency the authors thought best to exclude dates from being
considered IsNumeric, ie depending on the string the date may or may not
simply coerce to a number.
Concerning the Integer divisor: I had been aware of it's Banker's Rounding
nature for a long time but was surprised once to be hit with something like
this:
n = 4 \ 0.5 ' error div by zero
Regards,
Peter T
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx> wrote in
message news:%23HuPEBtVIHA.4696@xxxxxxxxxxxxxxxxxxxxxxx
See all my inline comments....I
ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")
Wow !
Yeah, that was pretty much my reaction when it first dawned on me that
IsNumeric didn't do what I thought it did. There are a lot of answers that
posted in my early days of volunteering in the compiled VB newsgroups thatwas
include the IsNumeric function as a "number proofer". Then, one day, by
chance, I typed in something like 12e3 into a TextBox in order to test the
error handling section of some code I wrote and, lo-and-behold, no error
generated. It took a few seconds for it to dawn on me that the 4-characterinvestigation
garbage "number" I thought I typed in was really an actual 5-digit number
(12e3=12000) to the IsNumeric function. That set me off on my
of the IsNumeric function which led to my publish article that I cited inmy
last post.numbers,
While we are talking about surprises like what IsNumeric considers
VB/VBA contains lots of such surprises. Let me give you another one that IOn
had misunderstood at first... Integer Division (where you use the backward
slash to for the division sign). In the beginning, I had thought (like I
have found many people do) that A\B was a sort of short-hand for Int(A/B).
IT IS NOT! In Integer Division, if A and/or B are floating point numbers,
those numbers are rounded first, BEFORE the integer division takes place.
top of that, the rounding method used is the one known as Banker'sRounding
(where numbers ending in 5 that are being rounded up to the previous digitwas
position round to the nearest even digit). Here is the problem... in my
beginnings with VB, I had figured (as most people still do) that 4.5\1.5
equivalent to Int(4.5/1.5) and that the answer would be 3; but IT IS NOT,the
rather, the answer it 2! The 4.5 is rounded to 4 before the division and
1.5 is rounded to 2 before the division... only then are they divideda
producing 2 (from this...4/2) as the answer.
Actually, as my currency is not USD I did this -
s = "($1,23,,3.4,,,5,,E67$)"
s = Replace(s, "$", Application.International(xlCurrencyCode))
Raised my curiosity -
Dim d as double
on error resume next
d = s ' try to coerce
If err.number then
err.clear
else
blnIsNumeric = true
end if
With the your string, Internationalized if necessary, it can be coerced
and somehow returns -1.23345E+70
The commas are completely ignored... the string becomes -123345E+67 which,
in scientic notation, is simplified to -1.23345E+70.
However -
v = application.Evaluate(s) ' Error 2015
Which leads me to wonder if the following in help -
"Returns a Boolean value indicating whether an expression can be
evaluated as a number"
might be better written as "- can be COERCED as a number"
I think the "evaluated" part refers to the Cxxx converter such as CLng,
CDbl, etc. Perhaps you didn't know... try printing out (in the Immediate
window) that same string inside a CDbl function call....
s = "($1,23,,3.4,,,5,,E67$)"
s = Replace(s, "$", Application.International(xlCurrencyCode))
Print CDbl(s)
Yep, it prints out -1.23345E+70. If IsNumeric returns True for a string
expression, then CInt, CLng, CCur, CSng and CDbl will accept the string as
valid argument and convert it to a number (assuming that number is smallnot
enough to fit in the indicated data type). Cute, huh?
Not sure why a Date is not considered as numeric although it
can 'sometimes' if not typically be coerced to a double.
I never thought to try Date before. I think the answer is in what the date
represents. As a String, something like "6/7/08" (or even "6/7/2008") is
really a number of any sort, so IsNumeric reports False for it and CDblwill
error out on it. Odd though, if you use the date delimiter symbols (#)
around it instead (that is, #6/7/08# or #6/7/2008#), CDbl convert it, but
IsNumeric will still report False for it. Very odd indeed.
Rick
.
- Follow-Ups:
- Re: Another VBA bug
- From: Rick Rothstein \(MVP - VB\)
- Re: Another VBA bug
- From: Peter T
- Re: Another VBA bug
- References:
- Another VBA bug
- From: Don Wiss
- Re: Another VBA bug
- From: Rick Rothstein \(MVP - VB\)
- Re: Another VBA bug
- From: Peter T
- Re: Another VBA bug
- From: Rick Rothstein \(MVP - VB\)
- Another VBA bug
- Prev by Date: Re: Auto start Macro to go to today's date in worksheet
- Next by Date: RE: Range Formula
- Previous by thread: Re: Another VBA bug
- Next by thread: Re: Another VBA bug
- Index(es):
Relevant Pages
|