Re: Another VBA bug
- From: "Rick Rothstein \(MVP - VB\)" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Mon, 14 Jan 2008 16:39:32 -0500
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.
Well, I think both the Cxxx functions and IsNumeric are to "generous" in what they consider numbers. Why is a number with a minus sign in both the front **and** back considered a valid number?
However I still suspect Help might be better written along the lines I
suggested using the word 'coerced' vs 'evaluated',
Agreed
''' 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'
In your last statement, the variable 's' is a Variant of sub-type String, but that String value is no different to the assignment than a/b/c would be, so I understand that type mismatch error.
btw, behind the scenes is there any difference between doing
dbl = s vs dbl = cDbl(s) ?
Well, technically, I think yes. Using CDbl specifically casts 's' as a Double before the assignment is made. Just assigning 's' directly to 'dbl' means VB's type coercion mechanism has to decide what to do... while I know it doesn't do this, it could theoretically cast it internally to a Single (losing precision) first before deciding it should be a Double. As I said, I'm positive it doesn't do this, but why leave the job to chance when you know what it should be?
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.
What I don't understand is why IsNumeric(Now) returns False while CDbl(Now) evaluates to a value (and, of course, the example I gave earlier). If IsNumeric is a "proofer" for the Cxxx functions, why not handle this also?
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
Yeah, I got caught with that one too. As a point of information, VB/VBA uses Banker's Rounding in every situation where rounding is necessary EXCEPT inside the Format function. The Format function uses what I consider to be normal rounding rules (5's are always rounded upward)....
Print Format(5/2, "0") ====> 3
Rick
.
- Follow-Ups:
- 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\)
- Re: Another VBA bug
- From: Peter T
- Another VBA bug
- Prev by Date: Re: change background color of cell as data in column changes
- Next by Date: Re: Deleting cells in a row
- Previous by thread: Re: Another VBA bug
- Next by thread: Re: Another VBA bug
- Index(es):
Relevant Pages
|