Text to columns, multiple spaces and columns

leebauman

Board Regular
Joined
Jul 1, 2004
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Hello, I need to extract what is in B2 to reorganize only information to the right and one space ahead of each "X". Example below.

Any help is much appreciated!

Sample data:
Book1
AB
1Order#Item
2123452X ABC 3X 123 1X Apple 6X Banana 9X Dog
Sheet1


Desired output:
Book1
AB
1Order #Item
212345ABC
312345123
412345Apple
512345Banana
612345Dog
Sheet2
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about
+Fluff 1.xlsm
ABCDE
1Order#Item
2123452X ABC 3X 123 1X Apple 6X Banana 9X Dog12345ABC
312345123
412345Apple
512345Banana
612345Dog
7
Master
Cell Formulas
RangeFormula
D2:D6D2=IF(E2#<>"",A$2,"")
E2:E6E2=FILTERXML("<k><m>"&SUBSTITUTE(SUBSTITUTE(B2,"X ","</m><x>")," ","</x><m>")&"</x></k>","//x")
Dynamic array formulas.
 
Upvote 0
That works on the sample data, but not on my broader set which includes thousands of orders and item sets. Here's a better example:

Sample data:
Book3
AB
1Order#Item
2123452X ABC 3X 123 1X Apple 6X Banana 9X Dog
3987653X Pear 2X Orange
4111112X Lemon
5222228X Almond 3X Walnut
Sheet1


Desired output:
Book3
AB
1Order#Item
212345ABC
312345123
412345Apple
512345Banana
612345Dog
798765Pear
898765Orange
911111Lemon
1022222Almond
1122222Walnut
Sheet2


Thank you!
 
Upvote 0
You can try this VBA solution. Running it will turn this...

Book1 (version 2).xlsb
AB
1Order#Item
2123452X ABC 3X 123 1X Apple 6X Banana 9X Dog
3987653X Pear 2X Orange
4111112X Lemon
5222228X Almond 3X Walnut
Sheet3


into this...

Book1 (version 2).xlsb
AB
1Order#Item
212345ABC
312345123
412345Apple
512345Banana
612345Dog
798765Pear
898765Orange
911111Lemon
1022222Almond
1122222Walnut
Sheet3


VBA Code:
Sub SPLITX()
Dim r As Range:         Set r = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")
Dim SP() As String
Dim tmp As String

With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(\s?\dX\s)"
    For i = LBound(AR) To UBound(AR)
        tmp = .Replace(AR(i, 2), "@")
        SP = Split(tmp, "@")
        For j = 1 To UBound(SP)
            AL.Add AR(i, 1) & ";" & SP(j)
        Next j
    Next i
End With

r.ClearContents

With Range("A2").Resize(AL.Count, 1)
    .Value = Application.Transpose(AL.toArray)
    .TextToColumns DataType:=xlDelimited, semicolon:=True
End With
End Sub
 
Upvote 0
I get an error when I run the Macro. Am I entering the VBA incorrectly? Thanks.
1615226117655.png
 
Upvote 0
Probably means you don't have .Net Framework installed on your computer. Try this instead.

VBA Code:
Sub SPLITX()
Dim r As Range:         Set r = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim SD As Object:       Set SD = CreateObject("Scripting.Dictionary")
Dim SP() As String
Dim tmp As String

With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(\s?\dX\s)"
    For i = LBound(AR) To UBound(AR)
        tmp = .Replace(AR(i, 2), "@")
        SP = Split(tmp, "@")
        For j = 1 To UBound(SP)
            SD.Add AR(i, 1) & ";" & SP(j), 1
        Next j
    Next i
End With

r.ClearContents

With Range("A2").Resize(SD.Count, 1)
    .Value = Application.Transpose(SD.keys())
    .TextToColumns DataType:=xlDelimited, semicolon:=True
End With
End Sub
 
Upvote 0
Solution
───────────────────░█▓▓▓█░▇▆▅▄▃▂
──────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
─────────────────░█▓▓▓▓▓█░▇▆▅▄▃▂
──────────░░░───░█▓▓▓▓▓▓█░▇▆▅▄▃▂
─────────░███░──░█▓▓▓▓▓█░▇▆▅▄▃▂
───────░██░░░██░█▓▓▓▓▓█░▇▆▅▄▃▂
──────░█░░█░░░░██▓▓▓▓▓█░▇▆▅▄▃▂
────░██░░█░░░░░░█▓▓▓▓█░▇▆▅▄▃▂
───░█░░░█░░░░░░░██▓▓▓█░▇▆▅▄▃▂
──░█░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░░░░█░░░░░░░░█▓▓▓█░▇▆▅▄▃▂
──░█░░█░░░█░░░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░█░░░░██░░░░░░█▓▓█░▇▆▅▄▃▂
─░█░░░░█░░░░░██░░░█▓▓▓█░▇▆▅▄▃▂
─░█░█░░░█░░░░░░███▓▓▓▓█░▇▆▅▄▃▂
░█░░░█░░░██░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░█░░░░█████▓▓▓▓▓█░▇▆▅▄▃▂
░█░░░░░█░░░░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
░█░█░░░░██░░░░█▓▓▓▓▓█░▇▆▅▄▃▂
─░█░█░░░░░████▓▓▓▓██░▇▆▅▄▃▂
─░█░░█░░░░░░░█▓▓██▓█░▇▆▅▄▃▂
──░█░░██░░░██▓▓█▓▓▓█░▇▆▅▄▃▂
───░██░░███▓▓██▓█▓▓█░▇▆▅▄▃▂
────░██▓▓▓███▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓█▓▓▓█░▇▆▅▄▃▂
──────░█▓▓▓▓▓▓▓▓▓▓▓▓▓█░▇▆▅▄▃▂
 
Upvote 0

Forum statistics

Threads
1,216,727
Messages
6,132,353
Members
449,720
Latest member
NJOO7

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