Parse AlphaNumeric String

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
Objective: To create a list of numbers based a cell which are present in an alpha-numeric string

Issues: "-" shows up at times without the context of numbers (i.e. MS-DRG)
Issues: I am not interested in float numbers (i.e. 37.63-37.66)

'Example data in cells (row 1 = A1, row 2 =A2, row 3 = A3, row 4 = A4)
MS-DRGs 767-768, 774-775
Rev 115, 125, 135, 145, 155, 655-656
Rev 174
MS-DRG 001 with ICD-9 37.63-37.66, 37.52

'Desired Results (row 1 = B1, row 2 =B2, row 3 = B3, row 4 = B4)
767,767,768,774,775
115, 125, 135, 145, 155, 655, 656
174
001

Any help would be appreciated.
 
Give this macro a try...
Code:
Sub GetNumericRanges()
  Dim X As Long, Y As Long, Z As Long, LastRow As Long, FirstDigit As Long, FirstLetter As Long
  Dim Pages As String, Numbers() As String, sRange() As String
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  For X = 1 To LastRow
    Pages = Cells(X, "A").Value
    FirstDigit = Evaluate("MIN(FIND(ROW(1:10)-1,""" & Pages & """&""0123456789""))")
    For Z = FirstDigit To Len(Pages) + 1
      If Mid(Pages & "x", Z, 1) Like "[a-z]" Then
        FirstLetter = Z
        Exit For
      End If
    Next
    Pages = Replace(Mid(Pages, FirstDigit, FirstLetter - FirstDigit), " ", "")
    Numbers = Split(Pages, ",")
    For Z = 0 To UBound(Numbers)
      If Numbers(Z) Like "*-*" Then
        sRange = Split(Numbers(Z), "-")
        Numbers(Z) = ""
        For Y = sRange(0) To sRange(1)
          Numbers(Z) = Numbers(Z) & "," & Y
        Next
        Numbers(Z) = Mid(Numbers(Z), 2)
      End If
    Next
    Cells(X, "B").Value = "'" & Join(Numbers, ", ")
  Next
End Sub

Rick -

This worked perfectly...Fantastic job.
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
my code excludes numbers containing a decimal point. as for numbers between the bookmarks of the
dash, that wasn't requested in the original post.

JoeMo -

Thank you as well for your solution and for responding so quickly...
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top