Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Avoid Type Mismatch Error when using CDBL()

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Avoid Type Mismatch Error when using CDBL()

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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Avoid Type Mismatch Error when using CDBL()

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Avoid Type Mismatch Error when using CDBL()

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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic Error Run Time Error, Type Mismatch Meg Partridge Excel Discussion (Misc queries) 12 September 10th 08 06:10 PM
runtime error 13 - type mismatch error in Excel 97 on Citrix Kevin Maher Excel Programming 7 March 8th 08 11:48 AM
Conditional Formatting - Run Time Error '13' Type Mismatch Error ksp Excel Programming 0 July 11th 06 07:06 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 AM


All times are GMT +1. The time now is 07:08 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"