+ Reply to Thread
Results 1 to 4 of 4

Extract Exact 6 Digit Number From String

Hybrid View

  1. #1
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Extract Exact 6 Digit Number From String

    Hi,

    I have a data set with combination of alphabets and numbers in Column A. I would like to find a formula or vb codes that can extract the string with exact 6 digital in column B. For example

    TestData 123456 TestTest ---> Return 123456
    TestData 123456789 TestTest ---> Return Blank (nothing)
    TestData 244123456 TestTest 987654 ---> Return 987654


    Any assistance is appreciated.

    Thanks.
    Last edited by tt388; 02-11-2013 at 12:51 PM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Extract Exact 6 Digit Number From String

    A custom function is one way:
    Function Six(v As Variant)
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "\b\d{6}\b"
        If .Test(v) Then
            Six = .Execute(v)(0)
        Else
            Six = vbNullString
        End If
    End With
    
    End Function
    E.g. =Six (A1)

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Extract Exact 6 Digit Number From String

    Function Get6Digits(r As String)
       Dim matches
       With CreateObject("vbscript.regexp")
          .Pattern = "\b\d{6}\b"
          .Global = True
          Set matches = .Execute(r)
          If matches.Count > 0 Then
             Get6Digits = matches(0)
          Else
             Get6Digits = ""
          End If
       End With
    End Function
    usage:
    =Get6Digits(A1)
    for instance
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Registered User
    Join Date
    03-29-2007
    MS-Off Ver
    365
    Posts
    70

    Re: Extract Exact 6 Digit Number From String

    Thanks both. It works exactly what I am looking for.

+ 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