Extract Alpha Numeric data VBA

decadence

Well-known Member
Joined
Oct 9, 2015
Messages
525
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2010
  5. 2007
Platform
  1. Windows
Hi, I am trying to split Alpha Numeric data into separate columns but not having any luck. Can someone Help with this.

What I would like it to look like
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]
Reference[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A30[/TD]
[TD]A[/TD]
[TD]30[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A30_1[/TD]
[TD]A[/TD]
[TD]_[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A30_1_2[/TD]
[TD]A[/TD]
[TD]_[/TD]
[TD]1[/TD]
[TD]_[/TD]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A_30[/TD]
[TD]A[/TD]
[TD]_[/TD]
[TD]30[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A_30_1[/TD]
[TD]A[/TD]
[TD]_[/TD]
[TD]30[/TD]
[TD]_[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]A_30_1_2[/TD]
[TD]A[/TD]
[TD]_[/TD]
[TD]30[/TD]
[TD]1[/TD]
[TD]_[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Code:
Public Sub ExtractAlphaNumeric()

Dim lastRow As Long
Dim thisRow As Long
Dim nextCol As Long
Dim currentGroup As String
Dim currentType As Long
Dim thisType As Long
Dim thisChar As Long

' Get the last row
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

' Process all rows
For thisRow = 2 To lastRow
    ' Next column to enter a value
    nextCol = 2

    ' Nothing in the current group or type
    currentGroup = ""
    currentType = 0
    
    ' Look at all characters in the string
    For thisChar = 1 To Len(Cells(thisRow, 1).Value)
        ' Determine the "type" of character
        Select Case UCase$(Mid$(Cells(thisRow, 1).Value, thisChar, 1))
            Case "A" To "Z"
                thisType = 1
            Case "0" To "9"
                thisType = 2
            Case Else
                thisType = 3
        End Select
        
        ' Have we changed types?
        If currentType <> thisType Then
            ' Do we have something in the current group?
            If currentGroup <> "" Then
                ' Populate the next column with the current group
                Cells(thisRow, nextCol).Value = currentGroup
                currentGroup = ""
                nextCol = nextCol + 1
            End If
        End If
        
        ' Accumulate the character into the current group and set the group type
        currentGroup = currentGroup & Mid$(Cells(thisRow, 1).Value, thisChar, 1)
        currentType = thisType
    Next thisChar
    
    ' Anything remaining needs to be populated into the next column
    If currentGroup <> "" Then Cells(thisRow, nextCol).Value = currentGroup
Next thisRow

End Sub

WBD
 
Upvote 0
This works Perfectly, Thank you
Here is another macro that you may want to consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub ExtractAlphaNumeric()
  Dim X As Long, Cell As Range, Txt As String, Data As Variant
  Data = Range("A2", Cells(Rows.Count, "A").End(xlUp))
  For Each Cell In Range("A2", Cells(Rows.Count, "A").End(xlUp))
    Txt = Cell.Value
    For X = Len(Txt) - 1 To 1 Step -1
      If Mid(Txt, X, 2) Like "[A-Za-z]#" Or Mid(Txt, X, 2) Like "#[A-Za-z]" Then Txt = Application.Replace(Txt, X + 1, 0, "_")
    Next
    Cell.Value = Replace(Txt, "_", "|_|")
  Next
  Range("A2", Cells(Rows.Count, "A").End(xlUp)).TextToColumns Range("B1"), xlDelimited, , , False, False, False, False, True, "|", Array(1, 4)
  Range("A2").Resize(UBound(Data)) = Data
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Hi Rick, your code works great except when cell values that contain no underscores will end up having them once split out, so A30 will split out to A _ 30
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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