Splitting pipe delimited cell into named columns

phil6011

New Member
Joined
Jan 26, 2008
Messages
15
Hi all

Hoping someone may be able to help something I'm stumped on.

We have a hierarchy file where the core data comes in a pipe delimited cell like below.

network_CAR|subnetwork_GARAGE|subnetwork_DEALER|KMBINDIA|KMBINDIA_1|KMBINDIA_2|KMBINDIA_3

I need something that takes this and outputs like the below


networksubnetworklocation
CARGARAGE|DEALERKMBINDIA|KMBINDIA_1|KMBINDIA_2|KMBINDIA_3

<tbody>
</tbody>


Everything other than 'location' codes is proceeded by columnname_XXX i.e. 'network_xxx' means Network is the name of the column and everything after the underscore is the value

Where the 'columnname' is repeated more than once it should only create one column for that name and then pipe-delimit all values referenced as in the subnetwork 'GARAGE|DEALER' example

Each row isn't necessarily consistent in what values it has either, some may skip the Subnetwork_XXX and just have Network_xxx & location values for example

Feels like I'm asking a potentially impossible task but wanted to check for sure.

Any help much appreciated!

Thanks
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I believe you can do the following:
Open the txt file in Excel. (This should put everyting in column A as a long pipe-delimited text field.
Next, under the Data heading, choose Text to Columns.
In the resulting wizard, choose the option for Delimited and click Next
In the next screen put a pipe character in the field next to Other (and make sure other is checked)
You can look into the last page of the wizard, but likely at this point you can just click FINISH.

..voila. You should have the output you are looking for.


EDIT: I need to read the whole request before i post. :ROFLMAO:
Let me think on this a little more. Are you opposed to a VBA solution because I suspect that is what will be required.
 
Last edited:
Upvote 0
OK.. Here is a VBA solution that worked with the limited testing I was able to perform including multiple network, no networks, multiple subnetworks, no subnetworks.

Let me know if this doesn't do what you are needing.

Code:
Sub ExpandPipeNetwork()
Dim srcSht As Worksheet
Dim dstSht As Worksheet
Dim firstsrcROW As Long, srcCOL As String
Dim dstROW As Long
Dim arr As Variant
Dim i As Long, arrI As Long
Dim strNetwork As String, strSubNetwork As String, strLocation As String


'***EDIT values in this section***


Set srcSht = Sheets("Sheet1")   'adjust this to the name of the sheet where the pipe delimited text exists


firstsrcROW = 2                 'adjust this based on the row where the first value exists (e.g. 1 if no headers)


srcCOL = "A"                    'the column where the pipe-delimited text exists


Set dstSht = Sheets("Sheet2")   'adjust this to the name of the sheet where you want to store the results
                                'PLEASE NOTE ALL EXISTING DATA ON THE DESTINATION SHEET WILL BE ERASED
                                
'****SHOULDN'T NEED TO EDIT BELOW THIS POINT***






dstSht.Cells.Clear
dstSht.Range("A1:C1").Value = Array("network", "subnetwork", "location")
dstROW = 2


For i = firstsrcROW To srcSht.Range("A" & srcSht.Rows.Count).End(xlUp).Row    'iterate through all src rows


    arr = Split(srcSht.Cells(i, srcCOL), "|")
    strNetwork = ""
    strSubNetwork = ""
    strLocation = ""
    
    For arrI = LBound(arr) To UBound(arr)
        
        If InStr(arr(arrI), "subnetwork_") > 0 Then
            strSubNetwork = strSubNetwork & IIf(strSubNetwork = "", "", "|") & Replace(arr(arrI), "subnetwork_", "")
        ElseIf InStr(arr(arrI), "network_") > 0 Then
            strNetwork = strNetwork & IIf(strNetwork = "", "", "|") & Replace(arr(arrI), "network_", "")
        Else
            strLocation = strLocation & IIf(strLocation = "", "", "|") & arr(arrI)
        End If
    Next arrI
    
    dstSht.Cells(dstROW, "A").Value = strNetwork
    dstSht.Cells(dstROW, "B").Value = strSubNetwork
    dstSht.Cells(dstROW, "C").Value = strLocation
    dstROW = dstROW + 1
Next i


dstSht.UsedRange.EntireColumn.AutoFit   'AutoFit column widths
MsgBox "DONE"


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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