It's mostly that Val is stuck in my memory from about 30 years of use,
and CLng is much more recent. You know the short-term memory is the
first to go. (My wife calls hers "momnesia".)
In most cases, if you assign the result of Val to a Long variable, the
result is identical to what you get from CLng, including the rounding
of non-integers, and the results will compare as equal.
There are some subtle differences, though.
- If the string contains anything other than digits and a decimal
point, Val will evaluate the start of the string up to (but not
including) the non-numeric character. It will also strip out and
ignore spaces, tabs, and line feeds. In the same situations, CLng will
throw a type mismatch error, which needs to be trapped with On Error.
This is not to say that Val is always superior. If you don't carefully
validate the input, Val can give you a number that looks reasonable
but is wrong, where CLng would clearly say "this is garbage".
- Both functions will accept arguments in hexadecimal notation. But
where CLng interprets all hex arguments as positive numbers, Val
interprets 4-digit hex numbers larger than 7FFF as negative numbers
(because the highest-order bit is considered to be a sign bit). You
won't run into this one very often!
Jay,
Are there advantages to Val(someString) over Clng(someString)?
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jay Freedman said:
Hi Ed,
Yes, you're going to need VBA. The Find mechanism itself can't do
comparisons of the values of digits. You need something like this:
Dim myRange As Range
Dim myVal As Long
Set myRange = ActiveDocument.Range
FoundIt = False
With myRange.Find
.MatchWildcards = True
.Text = "App A, Item No. ([0-9]{2})"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
myVal = Val(Right$(myRange.Text, 2))
If (60 <= myVal) And (myVal <= 81) Then
myRange.Select ' or do something else
Exit Do
End If
Loop
End With
--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
Ed wrote:
I'm trying to use Find (Ctrl+F) with wildcards to find a certain
string ending in a number between 60 and 81. I tried
(App A, Item No. )([60-81]{2})
but it found "App A, Item No. 10". I looked on the Word FAQ page
for Wildcards, but couldn't find how to restrict the number to
"more than this but less than that".
Can this be done with Find, or do I need to go into VBA?
Ed