PC Review


Reply
Thread Tools Rate Thread

Avoid Type Mismatch Error when using CDBL()

 
 
ExcelMonkey
Guest
Posts: n/a
 
      12th May 2009
Hi I am testing for whether or not a string variable is a number or not.
When I try:

IsNumeric(CDbl(var1))

It fails giving a Type Mismatch error when the variable is actually not a
number. I would like this to return a True/False. How do I do this?

Thanks

EM
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      13th May 2009
You could try:

if isnumeric(var1) then

But be aware that VBA's isnumeric is very forgiving. If the string looks like
it could be interpreted as a number, then you'll see True.

"9E3"
(9*10*10*10 in scientific notation)






ExcelMonkey wrote:
>
> Hi I am testing for whether or not a string variable is a number or not.
> When I try:
>
> IsNumeric(CDbl(var1))
>
> It fails giving a Type Mismatch error when the variable is actually not a
> number. I would like this to return a True/False. How do I do this?
>
> Thanks
>
> EM


--

Dave Peterson
 
Reply With Quote
 
ExcelMonkey
Guest
Posts: n/a
 
      13th May 2009
That does not work either

?IsNumeric(Val("Summary"))
True

I will use a Regex function which returns a boolean:

If ReturnWordOnly("50") = True Then
'Do something
End if

Private Function ReturnWordOnly(y As String) As Boolean
Dim Match As Boolean
Dim objRegExp As Object
Dim tempstring As Variant
Dim Counter As Double

Set objRegExp = CreateObject("Vbscript.RegExp")
objRegExp.Global = True
objRegExp.IgnoreCase = IgnoreCase
objRegExp.MultiLine = MultiLine
objRegExp.Pattern = "\D+" 'word

Match = objRegExp.Test(y)

ReturnWordOnly = Match

End Function
"Dave Peterson" wrote:

> You could try:
>
> if isnumeric(var1) then
>
> But be aware that VBA's isnumeric is very forgiving. If the string looks like
> it could be interpreted as a number, then you'll see True.
>
> "9E3"
> (9*10*10*10 in scientific notation)
>
>
>
>
>
>
> ExcelMonkey wrote:
> >
> > Hi I am testing for whether or not a string variable is a number or not.
> > When I try:
> >
> > IsNumeric(CDbl(var1))
> >
> > It fails giving a Type Mismatch error when the variable is actually not a
> > number. I would like this to return a True/False. How do I do this?
> >
> > Thanks
> >
> > EM

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      13th May 2009
Why did you add the val function?

val("summary")
returns a 0

So isnumeric(0) is gonna be true.

How about just using:
?isnumeric("Summary")



ExcelMonkey wrote:
>
> That does not work either
>
> ?IsNumeric(Val("Summary"))
> True
>
> I will use a Regex function which returns a boolean:
>
> If ReturnWordOnly("50") = True Then
> 'Do something
> End if
>
> Private Function ReturnWordOnly(y As String) As Boolean
> Dim Match As Boolean
> Dim objRegExp As Object
> Dim tempstring As Variant
> Dim Counter As Double
>
> Set objRegExp = CreateObject("Vbscript.RegExp")
> objRegExp.Global = True
> objRegExp.IgnoreCase = IgnoreCase
> objRegExp.MultiLine = MultiLine
> objRegExp.Pattern = "\D+" 'word
>
> Match = objRegExp.Test(y)
>
> ReturnWordOnly = Match
>
> End Function
> "Dave Peterson" wrote:
>
> > You could try:
> >
> > if isnumeric(var1) then
> >
> > But be aware that VBA's isnumeric is very forgiving. If the string looks like
> > it could be interpreted as a number, then you'll see True.
> >
> > "9E3"
> > (9*10*10*10 in scientific notation)
> >
> >
> >
> >
> >
> >
> > ExcelMonkey wrote:
> > >
> > > Hi I am testing for whether or not a string variable is a number or not.
> > > When I try:
> > >
> > > IsNumeric(CDbl(var1))
> > >
> > > It fails giving a Type Mismatch error when the variable is actually not a
> > > number. I would like this to return a True/False. How do I do this?
> > >
> > > Thanks
> > >
> > > EM

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cdbl and Cstr with a "Type Mismatch" Error Philosophaie Microsoft Excel Programming 6 3rd Apr 2010 06:14 AM
CDbl and "Type mismatch" Philosophaie Microsoft Excel Programming 3 26th Mar 2010 02:00 AM
How do I avoid this type mismatch? Sathyaish Microsoft C# .NET 0 16th Nov 2008 01:04 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Microsoft Excel Programming 1 31st Oct 2005 08:20 PM
Type mismatch error (different than previous type mismatch?) Roberta Microsoft Access VBA Modules 3 9th Jan 2004 06:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:06 PM.