Forum Discussion
zackwilson11
Aug 17, 2024Copper Contributor
VBA Code to Split Text
Hello, My place of employee switched programs and there is currently not a way to extract data. I'm looking to insert a VBA code that splits the Job Title and Job Req # into seperate cells. ...
- Aug 17, 2024
Select the range before running the following macro:
Sub SplitText() Dim r As Range Dim s As String Dim re As Object Dim m As Object Dim p As Long Application.ScreenUpdating = False ' Optional - insert column to the right Selection.Offset(0, 1).EntireColumn.Insert Set re = CreateObject(Class:="VBScript.RegExp") re.Pattern = "P\d" For Each r In Selection s = r.Value Set m = re.Execute(s) If m.Count > 0 Then p = m(0).FirstIndex r.Value = Left(s, p) r.Offset(0, 1).Value = Mid(s, p + 1) End If Next r Set m = Nothing Set re = Nothing Application.ScreenUpdating = True End Sub
HansVogelaar
Aug 17, 2024MVP
Select the range before running the following macro:
Sub SplitText()
Dim r As Range
Dim s As String
Dim re As Object
Dim m As Object
Dim p As Long
Application.ScreenUpdating = False
' Optional - insert column to the right
Selection.Offset(0, 1).EntireColumn.Insert
Set re = CreateObject(Class:="VBScript.RegExp")
re.Pattern = "P\d"
For Each r In Selection
s = r.Value
Set m = re.Execute(s)
If m.Count > 0 Then
p = m(0).FirstIndex
r.Value = Left(s, p)
r.Offset(0, 1).Value = Mid(s, p + 1)
End If
Next r
Set m = Nothing
Set re = Nothing
Application.ScreenUpdating = True
End Subzackwilson11
Aug 17, 2024Copper Contributor
Thank you!!