ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Why is this a number? (https://www.excelbanter.com/excel-programming/350258-why-number.html)

Dave F.[_5_]

Why is this a number?
 
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.



Tom Ogilvy

Why is this a number?
 
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.

--
Regards,
Tom Ogilvy

"Dave F." wrote in message
...
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.





Dave F.[_5_]

Why is this a number?
 
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.


"Tom Ogilvy" wrote in message
...
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.

--
Regards,
Tom Ogilvy

"Dave F." wrote in message
...
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.







Tom Ogilvy

Why is this a number?
 
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



--
Regards,
Tom Ogilvy


"Dave F." wrote in message
...
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.


"Tom Ogilvy" wrote in message
...
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.

--
Regards,
Tom Ogilvy

"Dave F." wrote in message
...
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.










All times are GMT +1. The time now is 10:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com