Formula or UDF to split a cell value into rows within a new cell

mnty89

Board Regular
Joined
Apr 1, 2016
Messages
66
Hi All,

This is a little too much of a complex vba for me, and I didnt know how to describe it in a concise enough way to google it effectively. Right now it is a very repetitive copy paste, text to columns, transpose, filter process for each cell..

Basically I will get an excel cell value of one long text as in the example below. I want a formula or UDF, or VBA that will loop through the rows that have that long text, and turn it into cleaned up cell like the before and after below-

Before cell

Related PO: SPH Related PO: 0049-1637504-3804 Related PO: 0003-1725849-3804 Related PO: 0253-1982071-3804 Related PO: 0245-2082143-3804 Related PO: 0231-2890617-3804 Related PO: 0052-3375471-3804 Related PO: 0007-3521948-3804 Related PO: 0094-5321648-3804 Related PO: 0037-8544319-3804 Related PO: 0003-8940612-3804 Related PO: 0063-9546192-3804 Related PO: 0212-9684200-3804 Related PO: 0212-9823856-3804

After cell
0049-1637504-3804
0003-1725849-3804
0253-1982071-3804
0245-2082143-3804
0231-2890617-3804
0052-3375471-3804
0007-3521948-3804
0094-5321648-3804
0037-8544319-3804
0003-8940612-3804
0063-9546192-3804
0212-9684200-3804
0212-9823856-3804
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this out:

Code:
Public Sub CleanString()
Dim ary As Variant
    
ary = Split(" " & Range("A1").Value, " Related PO: ")
Range("A2").Resize(UBound(ary), 1).Value = Application.Transpose(ary)

End Subb

I used the below input (A1) and it spat out the results (beginning in A2). Note, A2 is blank because the string began with our delimeter.


Excel 2013/2016
A
1Related PO: SPH Related PO: 0049-1637504-3804 Related PO: 0003-1725849-3804 Related PO: 0253-1982071-3804 Related PO: 0245-2082143-3804 Related PO: 0231-2890617-3804 Related PO: 0052-3375471-3804 Related PO: 0007-3521948-3804 Related PO: 0094-5321648-3804 Related PO: 0037-8544319-3804 Related PO: 0003-8940612-3804 Related PO: 0063-9546192-3804 Related PO: 0212-9684200-3804 Related PO: 0212-9823856-3804
2
3SPH
40049-1637504-3804
50003-1725849-3804
60253-1982071-3804
70245-2082143-3804
80231-2890617-3804
90052-3375471-3804
100007-3521948-3804
110094-5321648-3804
120037-8544319-3804
130003-8940612-3804
140063-9546192-3804
150212-9684200-3804
Sheet1
 
Last edited:
Upvote 0
Wow.. you are fast/a god..

Is there any way to make it format to return the result in one single cell but one under another as opposed to row after row?
 
Upvote 0
Wow.. you are fast/a god..

Is there any way to make it format to return the result in one single cell but one under another as opposed to row after row?

Try:

Code:
Public Sub CleanString2()
Range("A2").Value = Replace(" " & Range("A1").Value, " Related PO: ", vbLf)
End Sub
 
Upvote 0
Assuming your single example is truly representative of your actual data, you can also do what you want with a formula...

=SUBSTITUTE(MID(SUBSTITUTE(SUBSTITUTE(A1,": ",":")," Related PO",""),16,LEN(A1)),":",CHAR(10))
 
Last edited:
Upvote 0
embarrassed to ask, but since it will take you 1 minute and me an hour to figure it out..

Can you tweak the second answer to start with A1, output into B1, and run as a loop until there are no more values in A?

I think it's a matter of declaring it as a variable and do until x= "" or something?
 
Upvote 0
Can you tweak the second answer to start with A1, output into B1, and run as a loop until there are no more values in A?
I am not clear which of these you want, so if the first is wrong, then try the second one...
Code:
[table="width: 500"]
[tr]
	[td]Sub CleanStrings()
  Dim Cell As Range, Ary As Variant
  For Each Cell In Columns("A").SpecialCells(xlConstants)
    Cell.Offset(, 1).Value = Replace(Mid(Cell.Value, InStr(Cell.Value, ":") + 2), " Related PO: ", vbLf)
  Next
End Sub[/td]
[/tr]
[/table]

Code:
[table="width: 500"]
[tr]
	[td]Sub CleanStrings()
  Dim Cell As Range, Ary As Variant
  For Each Cell In Columns("A").SpecialCells(xlConstants)
    Ary = Split(Mid(Cell.Value, InStr(Cell.Value, ":") + 2) & " Related PO: ", " Related PO: ")
    Cell.Offset(, 1).Resize(, UBound(Ary)).Value = Application.Index(Ary, 1, 0)
  Next
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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