Split text and numbers

JohnExcel222

New Member
Joined
Dec 19, 2018
Messages
35
Office Version
  1. 365
Hi there, this code splits the text and numbers into 2 columns. How can I preserve the spaces between the numbers?

Example
If the string contains: Word 2007 Excel 2016...
I would like to get Word Excel in one column and 2007 2016 in another column (and not 20072016)

Public Function SplitText(pWorkRng As Range, pIsNumber As Boolean) As String
Dim xLen As Long
Dim xStr As String
xLen = VBA.Len(pWorkRng.Value)
For i = 1 To xLen
xStr = VBA.Mid(pWorkRng.Value, i, 1)
If ((VBA.IsNumeric(xStr) And pIsNumber) Or (Not (VBA.IsNumeric(xStr)) And Not (pIsNumber))) Then
SplitText = SplitText + xStr
End If
Next
End Function
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try this adjustment to the code:
Rich (BB code):
Public Function SplitText(pWorkRng As Range, pIsNumber As Boolean) As String

    Dim xLen As Long
    Dim xStr As String
    Dim i As Long
    
    xLen = VBA.Len(pWorkRng.Value)
    
    For i = 1 To xLen
        xStr = VBA.Mid(pWorkRng.Value, i, 1)
        If ((VBA.IsNumeric(xStr) And pIsNumber) Or (Not (VBA.IsNumeric(xStr)) And Not (pIsNumber))) Then
            SplitText = SplitText + xStr
        End If
        If xStr = " " Then SplitText = SplitText + xStr
    Next i
    
End Function
 
Upvote 0
Solution
Try this adjustment to the code:
Rich (BB code):
Public Function SplitText(pWorkRng As Range, pIsNumber As Boolean) As String

    Dim xLen As Long
    Dim xStr As String
    Dim i As Long
   
    xLen = VBA.Len(pWorkRng.Value)
   
    For i = 1 To xLen
        xStr = VBA.Mid(pWorkRng.Value, i, 1)
        If ((VBA.IsNumeric(xStr) And pIsNumber) Or (Not (VBA.IsNumeric(xStr)) And Not (pIsNumber))) Then
            SplitText = SplitText + xStr
        End If
        If xStr = " " Then SplitText = SplitText + xStr
    Next i
   
End Function
awesome happy :) Thank you !!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
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