• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to split numbers and text to respective columns

RAM72

Member
Macro to split numbers and text to distinct columns

Expected results next to adjacent columns IN B 19054010 COLUMNS C: INFANT BREAD


Code:
Columns A
19054010 INFANT BREAD
19059090 BAKERIES
 
There are various ways to do so.

I have arranged some of them. you may use the same fn in vba too.
 

Attachments

  • Split Name.xlsx
    10.3 KB · Views: 7
HI
as I understand
please check the attachment
by the way it is not a macro but a UDF
hope it meets your requirement
 

Attachments

  • Split Name.xlsm
    15.1 KB · Views: 7
HI
as I understand
please check the attachment
by the way it is not a macro but a UDF
hope it meets your requirement

Hi

Just check in working but however could you look in an insight where there are numbers in the description in red
see attached
 

Attachments

  • Split Name_2.xlsm
    21.4 KB · Views: 5
Check this.

Just change the range.

Code:
Sub split()
    Range("A2:A9").TextToColumns Destination:=Range("D2"), DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 1), Array(8, 1))
End Sub
 
Try this
Code:
Sub Split()
    Dim Arr, I As Long, Str As String
   
    Arr = Range("A2", Cells(Rows.Count, 1).End(xlUp)).Value
   
    For I = LBound(Arr) To UBound(Arr)
        Cells(I + 1, 2) = VBA.Split(Arr(I, 1), " ")(0)
        Cells(I + 1, 3) = Replace(Arr(I, 1), VBA.Split(Arr(I, 1), " ")(0), "")
    Next I
End Sub
 
Try this
Code:
Sub Split()
    Dim Arr, I As Long, Str As String
  
    Arr = Range("A2", Cells(Rows.Count, 1).End(xlUp)).Value
  
    For I = LBound(Arr) To UBound(Arr)
        Cells(I + 1, 2) = VBA.Split(Arr(I, 1), " ")(0)
        Cells(I + 1, 3) = Replace(Arr(I, 1), VBA.Split(Arr(I, 1), " ")(0), "")
    Next I
End Sub



Thank you working as charm
:awesome::awesome::);):cool:
 
RAM72

You will have problem when 2nd set of characters contains the 1st set of characters with REPLACE function.

If you like to use Split function, it should be
Code:
Sub test()
    Dim r As Range
    For Each r In Range("a2", Range("a" & Rows.Count).End(xlUp))
        If r.Value Like "* *" Then
            r(, 2).Resize(, 2).Value = Split(r.Value, " ", 2)
        End If
    Next
End Sub
 
RAM72

You will have problem when 2nd set of characters contains the 1st set of characters with REPLACE function.

If you like to use Split function, it should be
Code:
Sub test()
    Dim r As Range
    For Each r In Range("a2", Range("a" & Rows.Count).End(xlUp))
        If r.Value Like "* *" Then
            r(, 2).Resize(, 2).Value = Split(r.Value, " ", 2)
        End If
    Next
End Sub

Thanks for this solution Jindon, working :awesome::awesome::):):)
 
Hi
Mr. Jindon perfection as ever.
as per Mr. yaser
there is minor thing which is ; there is a trailing space.
I did that but works for the text not for numbers


Code:
Sub yaser()
    Dim Arr, I As Long, Str As String
  
    Arr = Range("A2", Cells(Rows.Count, 1).End(xlUp)).Value
  
    For I = LBound(Arr) To UBound(Arr)
        Cells(I + 1, 2) = Trim(VBA.Split(Arr(I, 1), " ")(0))
        Cells(I + 1, 3) = Trim(Replace(Arr(I, 1), VBA.Split(Arr(I, 1), " ")(0), ""))
    Next I
End Sub
 
Last edited by a moderator:
Try add one line
Code:
For I = LBound(Arr) To UBound(Arr)
    Arr(I, 1) = Application.Trim(Arr(I, 1))  '<-- this line
    Cells(I + 1, 2) = Trim(VBA.Split(Arr(I, 1), " ")(0))
 
Back
Top