Numerical Data vs Text
I reversed your code to check vartype first, then isnumber. I also changed
Application.Isnumber to Worksheet.Isnumber
Vartype: 1.00
True
IsNumber: 3.01
True
Vartype: 1.00
False
IsNumber: 3.01
False
Vartype: 2.01
False
IsNumber: 2.01
False
As can be seen I got significanlty different results based on the
combination of how s was initialized (true or false) and whether activecell
contained a number or was blank.
With your initial code, I got results similar to you.
Worksheet as a qualifier vice application seems to be significantly faster.
--
Regards,
Tom Ogilvy
Harlan Grove wrote in message
...
"Tom Ogilvy" wrote...
What did your tests show?
...
Harlan Grove wrote in message
"Tom Ogilvy" wrote...
...
? application.IsNumber("88")
False
...
VarType(Rng.Value) = vbDouble
I didn't time them - yet.
Interesting looking deeper into this. When dealing with Range objects in
VBA, should one use the .Value or the .Value2 property? If you pass the
IsNumber method the .Value property of a cell containing a positive number
formatted as date/time, it'll return FALSE since the .Value would be
passed
to VBA as a Date type. If you pass the IsNumber method the .Value2
property,
on the other hand, it'll return TRUE. More interestingly, the IsNumber
method when passed a range reference alone, so neither the .Value nor the
.Value2 property, it seems to use the .Value2 property. In other words,
when
the active cell is initially formatted as General, then the formula =NOW()
is entered into it,
Debug.Print Application.IsNumber(ActiveCell) & Chr(13) & _
Application.WorksheetFunction.IsNumber(ActiveCell)
gives
True
True
Debug.Print Application.IsNumber(ActiveCell.Value) & Chr(13) & _
Application.WorksheetFunction.IsNumber(ActiveCell. Value)
gives
False
False
Debug.Print Application.IsNumber(ActiveCell.Value2) & Chr(13) & _
Application.WorksheetFunction.IsNumber(ActiveCell. Value2)
gives
True
True
Digression: does this mean .Value2 rather than .Value is the default
property? Or does it mean that the IsNumber method when passed a range
reference chooses to use the .Value2 property rather than the .Value
property?
Only the OP could say for sure, but I'd guess for this sort of thing, the
.Value2 property would be what's wanted. So on to profiling.
Given the profiling macro
Sub foo()
Const MAXITER As Long = 500000
Dim i As Long, s As Boolean, dt As Date, et As Date
s = True
dt = Now
For i = 1 To MAXITER
s = s And Application.IsNumber(ActiveCell.Value2)
Next i
et = Now
Debug.Print "IsNumber: " & Format(86640 * (et - dt), "0.00")
Debug.Print s
s = True
dt = Now
For i = 1 To MAXITER
s = s And (VarType(ActiveCell.Value2) = vbDouble)
Next i
et = Now
Debug.Print "VarType: " & Format(86640 * (et - dt), "0.00")
Debug.Print s
End Sub
the results are
IsNumber: 16.04
True
VarType: 4.01
True
on my machine. Looks like VarType plus a comparison operation is
significantly faster than the IsNumber method call. However, this leaves
the
deeper question of whether dates/times should be considered numbers.
|