VBA code to split a string into a table

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone, I need to split a string into a table as the picture describes. ask someone for help. Thank you
1675001222032.png
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Give this a go.

VBA Code:
Public Sub subSplitString()
Dim arr() As String
Dim rng As Range
    
    For Each rng In Range("D3", Cells(Rows.Count, "D").End(xlUp)).Cells
            
        arr = Split(Trim(Replace(Replace(rng.Value, " / ", " ", 1), "/", "", 1)), " ")
   
        With Cells(Rows.Count, "I").End(xlUp).Offset(1).Resize(UBound(arr) + 1, 1)
            .Value = Application.Transpose(arr)
            .Offset(0, -2).Value = rng.Offset(0, -2).Value
            .Offset(0, -1).Value = rng.Offset(0, -1).Value
        End With

    Next rng

End Sub
 
Upvote 0
Solution
An alternative solution is with Power Query
text new.xlsx
BCDEFGH
1INPUT
2TEXT 1TEXT 2DataTEXT 1TEXT 2Data
3XXX1A / A / CC / DDD /XXX1A
4YYYYY222AA / BB / CC / CC / EE / DD / FF / GG /XXX1A
5XXX1CC
6XXX1DDD
7YYYYY222AA
8YYYYY222BB
9YYYYY222CC
10YYYYY222CC
11YYYYY222EE
12YYYYY222DD
13YYYYY222FF
14YYYYY222GG
Sheet1


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(Source, {{"Data", Splitter.SplitTextByDelimiter("/", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Data"),
    #"Trimmed Text" = Table.TransformColumns(#"Split Column by Delimiter",{{"Data", Text.Trim, type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Trimmed Text", each ([Data] <> ""))
in
    #"Filtered Rows"
 
Upvote 0
I ask you to correct the code at this line :
arr = Split(Trim(Replace(Replace(rng.Value, " / ", " ", 1), "/", "", 1)), " ")

My data IS THIS EXAMPLE THE CODE is running wrong

A -1 / A -2 / CC / DDD /
Thank you

1675902009678.png
 
Upvote 0

Forum statistics

Threads
1,216,471
Messages
6,130,822
Members
449,595
Latest member
jhester2010

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