+ Reply to Thread
Results 1 to 7 of 7

Check if string contains a number

Hybrid View

mkvassh Check if string contains a... 09-25-2009, 07:52 AM
Shijesh Kumar Re: Check if string contains... 09-25-2009, 08:13 AM
Ron Coderre Re: Check if string contains... 09-25-2009, 08:16 AM
Shijesh Kumar Re: Check if string contains... 09-25-2009, 08:34 AM
Ron Coderre Re: Check if string contains... 09-25-2009, 08:41 AM
Shijesh Kumar Re: Check if string contains... 09-25-2009, 08:43 AM
mkvassh Re: Check if string contains... 09-25-2009, 08:45 AM
  1. #1
    Forum Contributor
    Join Date
    11-02-2007
    Location
    Bryne, Norway
    MS-Off Ver
    Microsoft Office Excel 2010
    Posts
    230

    Check if string contains a number

    Is there way in Excel VBA to check if a string contains a number, and then return TRUE or FALSE. Numbers can been anywhere in the string. See example below.

    EXPENDITURE DESCRIPTION  FALSE
    Gross Expenditure Costs              FALSE
    5.1.2 Other 0 0 5.047.549.103      TRUE
    Last edited by mkvassh; 09-25-2009 at 08:47 AM.

  2. #2
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717

    Re: Check if string contains a number

    Check this out

    http://office.microsoft.com/en-us/ex...549011033.aspx

  3. #3
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Check if string contains a number

    With A1 containing anything.

    Here are two approaches:
    Non-VBA:

    B1: =MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))<LEN(A1)

    Edited to include this shorter (but slightly less intuitive) alternative:
    B1: =MIN(SEARCH(ROW(1:10)-1,A1&ROW(1:10)-1))<LEN(A1)
    Using VBA:


    Function ContainsNum(cCell As Range) As Boolean
    Dim iCtr As Long
    
    For iCtr = 1 To Len(cCell.Text)
       ContainsNum = IsNumeric(Mid(cCell, iCtr, 1))
       If ContainsNum = True Then
          Exit Function
       End If
    Next iCtr
    End Function
    Usage:
    B1: =ContainsNum(A1)
    Does that help?
    Last edited by Ron Coderre; 09-25-2009 at 08:27 AM.
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  4. #4
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717

    Re: Check if string contains a number

    Alternative

    =SUMPRODUCT( IF(ISERROR(SEARCH({0,1,2,3,4,5,6,7,8,9},A1)),0,1))

  5. #5
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Check if string contains a number

    Quote Originally Posted by Shijesh Kumar View Post
    Alternative

    =SUMPRODUCT( IF(ISERROR(SEARCH({0,1,2,3,4,5,6,7,8,9},A1)),0,1))
    I think that would return the count of digits in the referenced cell.

    This would return 1 for cells containing numbers:

    B1: =MAX(--ISNUMBER(SEARCH({0,1,2,3,4,5,6,7,8,9},A1)))

  6. #6
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717

    Re: Check if string contains a number

    yes it will return count of digits..
    so we can modify it as below to return true or false


    = if( SUMPRODUCT( IF(ISERROR(SEARCH({0,1,2,3,4,5,6,7,8,9},A1)),0,1)) > 0 , true , false)
    One question thousand solution ;-)

  7. #7
    Forum Contributor
    Join Date
    11-02-2007
    Location
    Bryne, Norway
    MS-Off Ver
    Microsoft Office Excel 2010
    Posts
    230

    Re: Check if string contains a number

    It works. Thanks a lot :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1