combining column data with a twist

Boost001

New Member
Joined
Jun 21, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi there, I am trying to combine data in N columns into 2 columns where ColumnA is "\" separated, with ColumnB being the next entry to be added to ColumnA. To add to that, columnC should read either "Leaf" or "Branch" depending if this is the last column in the row.

So, my source data will look like (using , to denote new columns)

Mill1,Motor,Overheated
Mill1,Motor,Bearing
Mill1,Clutch,Disengaged
Mill1,In feed,No Feed

etc...

The output would be (the first column of the first row would be blank)
,Mill1,"Branch"
Mill1,Motor,"Branch"
Mill1\Motor,Overheated,"Leaf"
Mill1\Motor,Bearing,"Leaf"
Mill1,Clutch,"Branch"
Mill1\Clutch,Disengaged,"Leaf"
Mill1,In feed,"Branch"
Mill1\In feed,No Feed,"Leaf"

Hopefully this makes sense, I have attached an image of the before and after that I would like to see. I am not a programmer so have tried to use AI to generate some scripts for me, but they just aren't working and I don't know enough to debug them.
 

Attachments

  • Capture.PNG
    Capture.PNG
    27.9 KB · Views: 18

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi

Please check whether this is ok

This involves a series of steps to transform the input to the output format you want

Power Query would help, but have tried to do here using Excel

You may have the intermediate steps in hidden sheets/hidden columns

Tweaking columns.xlsx
ABCDEFGHIJKLMNOPQRS
1InputSl NoStep 1
2Mill1MotorOverheated1Mill1Mill1Mill1MotorMotorMill1Mill1\MotorOverheatedMotor
3Mill1MotorBearing2Mill1Mill1Mill1MotorMotorMill1Mill1\MotorBearingMotor
4Mill1ClutchDisengaged3Mill1Mill1Mill1ClutchClutchMill1Mill1\ClutchDisengagedClutch
5Mill1InfeedNo Feed4Mill1Mill1Mill1InfeedInfeedMill1Mill1\InfeedNo FeedInfeed
6
7
8Step 2Stacking, removing duplicates
9Mill1Mill100
100000
11Mill10MotorMotor
12Mill10ClutchClutch
13Mill10InfeedInfeed
14Mill1Mill1\MotorOverheatedMotor
15Mill1Mill1\MotorBearingMotor
16Mill1Mill1\ClutchDisengagedClutch
17Mill1Mill1\InfeedNo FeedInfeed
18
19
20
21
22Step 3Sorting
230000
24Mill1Mill100
25Mill10ClutchClutch
26Mill1Mill1\ClutchDisengagedClutch
27Mill10InfeedInfeed
28Mill1Mill1\InfeedNo FeedInfeed
29Mill1Mill1\MotorBearingMotor
30Mill10MotorMotor
31Mill1Mill1\MotorOverheatedMotor
32
33
34
35Step 4Remove 0
36Mill1Mill100
37Mill10ClutchClutch
38Mill1Mill1\ClutchDisengagedClutch
39Mill10InfeedInfeed
40Mill1Mill1\InfeedNo FeedInfeed
41Mill1Mill1\MotorBearingMotor
42Mill10MotorMotor
43Mill1Mill1\MotorOverheatedMotor
44
45
46
47Output (Step 5)Arrange columns
48 Mill1Branch
49Mill1ClutchBranch
50Mill1\ClutchDisengagedLeaf
51Mill1InfeedBranch
52Mill1\InfeedNo FeedLeaf
53Mill1\MotorBearingLeaf
54Mill1MotorBranch
55Mill1\MotorOverheatedLeaf
Sheet1
Cell Formulas
RangeFormula
F2:F5F2=A2
G2:G5G2=A2
M2:M5M2=B2
N2:N5N2=B2
P2:P5,R2:R5P2=A2
Q2:Q5Q2=A2&"\"&B2
S2:S5S2=B2
K2:K5K2=A2
F9:I17F9=UNIQUE(VSTACK(F2:I6,K2:N6,P2:S6))
F23:I31F23=SORTBY(F9#,F9:F17,1,I9:I17,1,H9:H17,1)
F36:I43F36=FILTER(F23#,F23:F31<>0)
F48:F55F48=IF(I36=0,"",IF(G36=0,F36,G36))
G48:G55G48=IF(F48="",G36,H36)
H48:H55H48=IF(ISNUMBER(SEARCH("\",F48,1)),"Leaf","Branch")
Dynamic array formulas.
 
Upvote 0
Thank you for the reply. I was looking at a scripted solution as I don't know how many columns and rows I may be given.
 
Upvote 0
so I spent a few days with Google and StackOverflow and managed to bash together a working script to do what I needed.
 
Upvote 0
so I spent a few days with Google and StackOverflow and managed to bash together a working script to do what I needed.
Would you mind posting your script/solution?
If you do that, it may help others in the future, and you can then mark that post as the solution to this question.
 
Upvote 0
So here is my code. I am ignoring the first row of the source data as that will contain some header information, and some specifics that I need in there which complicates things a little. I also have a check for invalid characters

VBA Code:
Private Sub ReplaceSpecialCharacters()
    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim specialChars As String
    Dim replaceChar As String
    
    ' Define the worksheet to be processed
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' Update "Sheet1" with your sheet name
    
    ' Define the range to be processed (assuming data starts from A1)
    Set rng = ws.UsedRange
    
    ' Define the special characters to be replaced
    specialChars = "*?;{}[]|\`'"""
    
    ' Define the character to replace special characters with
    replaceChar = "_" ' You can change this to whatever character you want
    
    ' Loop through each cell in the range
    For Each cell In rng
        If Not IsEmpty(cell.Value) Then
            ' Loop through each character in the cell's value
            For i = 1 To Len(cell.Value)
                ' Check if the character is a special character
                If InStr(specialChars, Mid(cell.Value, i, 1)) > 0 Then
                    ' Replace the special character with the desired character
                    cell.Value = Replace(cell.Value, Mid(cell.Value, i, 1), replaceChar)
                End If
            Next i
        End If
    Next cell
End Sub

Private Sub Concatenate()
    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    Dim LastRow As Long
    Dim LastCol As Long
    Dim i As Long
    Dim concatenatedValue As String
    Dim Columni As String
    Dim j As Long
    Dim k As Long
Set SourceRange = Application.Selection
    k = 1

    ' Set the source worksheets
    Set wsSource = ThisWorkbook.Sheets("Sheet1")
    
    ' Create a new sheet and rename it
    On Error Resume Next
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets("Sheet2").Delete
    On Error GoTo 0
    ActiveWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Sheet2"
    
    ' Set the target worksheets
    Set wsTarget = ThisWorkbook.Sheets("Sheet2")
    
    ' Find the last row with data in column A of the source sheet
    LastRow = wsSource.Cells(wsSource.Rows.Count, "B").End(xlUp).Row
    
    ' Loop through each row

    For i = 2 To LastRow
    LastCol = wsSource.Cells(i, wsSource.Columns.Count).End(xlToLeft).Column
    
    ' Initialize concatenatedValue for each row
    concatenatedValue = "Reason Tree\"
    
    ' adding root elements
            If k = 1 Then
                wsTarget.Cells(1, 2).Value = "Reason Tree"
                wsTarget.Cells(1, 4).Value = "Reason Tree Root"
                k = k + 1
            End If
        
        wsTarget.Cells(k, 1).Value = concatenatedValue
        ' Loop through each column in the current row
        For j = 2 To LastCol

            wsTarget.Cells(k, 1).Value = concatenatedValue
            wsTarget.Cells(k, 2).Value = wsSource.Cells(i, j).Value
            concatenatedValue = concatenatedValue & wsSource.Cells(i, j).Value & "\"
            If j < LastCol Then
                wsTarget.Cells(k, 4).Value = "Reason Tree Node"
            Else
                wsTarget.Cells(k, 4).Value = "Reason Tree Leaf"
                wsTarget.Cells(k, 3).Value = "Root Categories\" & wsSource.Cells(i, 1).Value
            End If

            k = k + 1
        Next j
        ' Remove the trailing "\" from the concatenated value
        concatenatedValue = Left(concatenatedValue, Len(concatenatedValue) - 1)
        For l = 1 To Step - 1
            Set EntireRow = wsTarget.Cells(i, 1).EntireRow
            If Application.WorksheetFunction.CountA(EntireRow) = 0 Then
                EntureRow.Delete
            End If
        Next l
    Next i
End Sub
Private Sub DeleteBlankRows()
  Dim SourceRange As Range
  Dim EntireRow As Range
Set SourceRange = Application.Selection
If Not (SourceRange Is Nothing) Then
    Application.ScreenUpdating = False
For i = SourceRange.Rows.Count To 1 Step -1
      Set EntireRow = SourceRange.Cells(i, 1).EntireRow
      If Application.WorksheetFunction.CountA(EntireRow) = 0 Then
        EntireRow.Delete
      End If
    Next
Application.ScreenUpdating = True
  End If
End Sub
Private Sub RemoveDuplicates()
    Dim B As Long
    B = Cells(Rows.Count, 1).End(xlUp).Row
    Range("A1:AA" & B).RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo
End Sub
Sub Run()
Call ReplaceSpecialCharacters
Call Concatenate
Call DeleteBlankRows
Call RemoveDuplicates
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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