Why is this a number?

D

Dave F.

Hi
I'm using VBA 6.3

If I pass 45- to this:

If IsNumeric(OffsetNo) = False Then
...

it returns True.

1) Why?

2) How do I prevent this happening?

Thanks in Advance.
 
T

Tom Ogilvy

Because Excel knows how to convert it to a number

? clng("45-")
-45

which is the defninition of isNumeric.

Many accounting applications indicate negatives by using the notation you
show.
 
D

Dave F.

Thanks for replying.


I'm running this within AutoCAD & don't have the Office Reference ticked.
I'm confused why it would be a specific Excel problem.
Isn't IsNumeric a general VB command?

Is there a way to prevent it happening?

I'm trying to allow only positive numeric values being typed & displayed in
a textbox:

If IsNumeric(OffsetNo) = False Then ' if there is a value in the
textbox & it's not a number
OffsetNo = Left$(OffsetNo, Len(OffsetNo) - 1) ' remove last typed
TxtBx_Dist.Value = OffsetNo ' & put it back in the box
End If

Cheers

Dave F.
 
T

Tom Ogilvy

I don't recall saying it is a specific Excel problem - Perhaps I should have
said VBA rather than Excel. Neither clng or IsNumeric are unique to excel.

If you want to check for a hypen as the last character, then check if a
hypen is the last character.

Another approach might be

if isnumeric(string) then
if clng(string) < 0 then
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top