You are here:

Excel/ISNUMBER function in Excel

Advertisement


Question
QUESTION: Hello Bob,
given that the ISNUMBER function returns false for a text string, even if the text string contains only numerical values, what is the best way to check whether a string contains only numerical values?

ie if it worked how I would like
=ISNUMBER("abc") would returns false, as it does
=ISNUMBER(123) would return true, as it does
but
=ISNUMBER("123") would return true, instead of false

ANSWER: =NOT(ISERROR(1*Range))
like
=NOT(ISERROR(1*A1)) or =NOT(ISERROR(1*"123"))
if A1 contained "123" then =NOT(ISERROR(1*"123")) would be =NOT(ISERROR(123)) which is =NOT(FALSE) which is TRUE.
If A contained "abc" then =NOT(ISERROR(1*"abc")) would be =NOT(ISERROR(#VALUE)) which is =NOT(TRUE) which is FALSE

HTH

---------- FOLLOW-UP ----------

QUESTION: Thanks Bob,

Now I'd like to do a similar thing in VBA.  I wrote this function to test arg,  if arg is a numeric string then the function should return a number, but if arg is non-numeric it should just return arg with no change.  But it doesn't work :(  It returns #VALUE when arg is not numeric, even when arg is "123".

Function ConvertNumber(arg As String)
If IsError(arg * 1) Then
   ConvertNumber = arg  'if arg cannot be recognised as a number then return arg without change
Else
   ConvertNumber = CDbl(arg)  'if arg can be recognised as a number then return the number as a value, not a string
End If
End Function

Answer
Function ConvertNumber(arg As String)
   On Error Resume Next
   n = arg * 1
   If Err.Number <> 0 Then
       ConvertNumber = arg  'if arg cannot be recognised as a number then return arg without change
   Else
       ConvertNumber = CDbl(arg)  'if arg can be recognised as a number then return the number as a value, not a string
   End If
End Function
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.