• Extracting Numbers from AlphaNumeric Strings

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Extracting Numbers from AlphaNumeric Strings

    Author
    Topic
    #468519

    Problem: In cell A1-A100, I have the following types of data: “ABC 1234-5678”, or “DEF 9012-3456 “, etc. In other words, there is text entered as the first, say, 1-25 characters of a cell, then anywhere from 1 to 50 spaces, then 4 numbers, then a hyphen, then 4 more numbers, then anywhere from 0 to 50 additional spaces. What I would like to do is extract from cells A1:A100 all of the numbers in their original format (i. e., including the hyphen, “1234-5678”, “9012-3456”) into cells B1:B100. Any ideas? Thanks in advance.

    Viewing 4 reply threads
    Author
    Replies
    • #1220982

      You could use this UDF:

      Code:
      Function GetNumbers(strInput As String) As String
          Dim RegExp As Object, match, matches
          Dim strPattern As String
          Set RegExp = CreateObject("vbscript.regexp")
          strPattern = "d{4}-d{4}"
          With RegExp
              .Global = True
              .IgnoreCase = True
              .Pattern = strPattern
               Set matches = .Execute(strInput)
              If matches.Count > 0 Then GetNumbers = matches(0)
          End With
      End Function
      
    • #1220984

      Thanks Rory. Now, how do I get it to work?

    • #1220989

      This post has been cross posted in Another forumwhere another solution has been offered.

      • #1220992

        This post has been cross posted in Another forumwhere another solution has been offered.

        Yes I did post on Eileen’s Lounge where my friend Hans resides now. Although a solution has been offered, it does not do the trick. All I would like to hear from Rory, or anyone for that matter, is how I can possibly get his UDF to work. Plain and simple! No hidden agendas here!

        • #1220993

          Yes I did post on Eileen’s Lounge where my friend Hans resides now. Although a solution has been offered, it does not do the trick. All I would like to hear from Rory, or anyone for that matter, is how I can possibly get his UDF to work. Plain and simple! No hidden agendas here!

          Does it need to be a UDF?

          You could use a formula such as
          =right(trim(A1),9)

    • #1220995

      Thanks Gfamily. That appears to work.

    • #1221004

      FWIW, you needed to copy that code into a module, then just use it as you would any other formula:
      =GetNumbers(A1)
      for example.
      I confess, I missed that the last characters would only be spaces not other text! 🙂

    Viewing 4 reply threads
    Reply To: Extracting Numbers from AlphaNumeric Strings

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: