Home

Condensing multiple outputs

%3CLINGO-SUB%20id%3D%22lingo-sub-837579%22%20slang%3D%22en-US%22%3ECondensing%20multiple%20outputs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-837579%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20column%20A%20with%20names%20that%20may%20repeat.%3C%2FP%3E%3CP%3EI%20have%20column%20B%20with%20a%20title%20related%20to%20the%20names%20in%20column%20A%20that%20can%20appear%20multiple%20times.%3C%2FP%3E%3CP%3EI%20want%20to%20be%20able%20to%20have%20multiples%20of%20the%20same%20name%20(from%20column%20A)%20collapsed%20into%20one%20row%20with%20the%20multiple%20titles%20(column%20B)%20appearing%20in%20multiple%20columns%20next%20to%20the%20names%2C%20with%20one%20title%20per%20column.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20any%20way%20to%20do%20this%3F%26nbsp%3B%20I've%20attached%20an%20example%20of%20what%20I%20would%20like%20to%20happen.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-837579%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-837893%22%20slang%3D%22en-US%22%3ERe%3A%20Condensing%20multiple%20outputs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-837893%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403617%22%20target%3D%22_blank%22%3E%40treecamel88%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20are%20open%20to%20a%20VBA%20solution%2C%20please%20try%20the%20following%20macro%20which%20will%20transform%20the%20data%20into%20the%20desired%20format.%3C%2FP%3E%3CP%3EIn%20the%20attached%2C%20click%20the%20button%20called%20%22Transform%20Data%22%20on%20Sheet1%20to%20get%20the%20data%20in%20the%20desired%20format.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20getNamesAndTitles()%0ADim%20wsData%20%20As%20Worksheet%0ADim%20x%20%20%20%20%20%20%20As%20Variant%0ADim%20dict%20%20%20%20As%20Object%0ADim%20str()%20%20%20As%20String%0ADim%20it%20%20%20%20%20%20As%20Variant%0ADim%20i%20%20%20%20%20%20%20As%20Long%0ADim%20dlr%20%20%20%20%20As%20Long%0ADim%20j%20%20%20%20%20%20%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0ASet%20wsData%20%3D%20Worksheets(%22Sheet1%22)%20%20%20'Sheet%20with%20data%0Ax%20%3D%20wsData.Range(%22A1%22).CurrentRegion.Value%0ASet%20dict%20%3D%20CreateObject(%22Scripting.Dictionary%22)%0A%0A%0AFor%20i%20%3D%202%20To%20UBound(x%2C%201)%0A%20%20%20%20If%20Not%20dict.exists(x(i%2C%201))%20Then%0A%20%20%20%20%20%20%20%20dict.Item(x(i%2C%201))%20%3D%20x(i%2C%202)%0A%20%20%20%20Else%0A%20%20%20%20%20%20%20%20dict.Item(x(i%2C%201))%20%3D%20dict.Item(x(i%2C%201))%20%26amp%3B%20%22_%22%20%26amp%3B%20x(i%2C%202)%0A%20%20%20%20End%20If%0ANext%20i%0A%0A'Clearing%20the%20data%20from%20columns%20H%3AJ%20before%20writing%20the%20transformed%20data%0A'If%20you%20want%20to%20replace%20the%20original%20data%20with%20the%20transformed%20data%2C%20replace%20H%3AJ%20with%20A%3AB%0AwsData.Columns(%22H%3AJ%22).ClearContents%0A%0Ai%20%3D%202%0AFor%20Each%20it%20In%20dict.keys%0A%20%20%20%20dlr%20%3D%20wsData.Range(%22H%22%20%26amp%3B%20Rows.Count).End(3)(2).Row%0A%20%20%20%20wsData.Range(%22H%22%20%26amp%3B%20dlr).Value%20%3D%20it%0A%20%20%20%20str%20%3D%20Split(dict(it)%2C%20%22_%22)%0A%20%20%20%20If%20UBound(str)%20%2B%201%20%26gt%3B%20j%20Then%20j%20%3D%20UBound(str)%20%2B%201%0A%20%20%20%20wsData.Range(%22I%22%20%26amp%3B%20dlr).Resize(%2C%20UBound(str)%20%2B%201).Value%20%3D%20str%0ANext%20it%0A%0A'Writing%20the%20transformed%20data%20in%20column%20H%20and%20adjacent%20columns%0A'To%20write%20data%20in%20column%20A%20and%20it's%20adjacent%20columns%2C%20replace%20H1%20with%20A1%20and%20I1%20with%20B1%0AwsData.Range(%22H1%22).Value%20%3D%20%22Name%22%0AwsData.Range(%22I1%22).Resize(%2C%20j).Value%20%3D%20%22Title%22%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839115%22%20slang%3D%22en-US%22%3ERe%3A%20Condensing%20multiple%20outputs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839115%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F394231%22%20target%3D%22_blank%22%3E%40Subodh_Tiwari_sktneer%3C%2FA%3E%26nbsp%3BThank%20you%20for%20your%20response!%3C%2FP%3E%3CP%3EIn%20attempting%20to%20understand%20your%20response%20and%20to%20be%20able%20to%20apply%20it%20to%20my%20actual%20problem%2C%20I%20have%20a%20couple%20of%20questions%3A%3C%2FP%3E%3COL%3E%3CLI%3E%3CP%3EIn%20this%20section%2C%20where%20x%20%3D%20wsData.Range(%22A1%22).CurrentRegion.Value%2C%20is%20A1%20where%20I%20can%20change%20where%20my%20input%20%22name%22%20column%20begins%3F%3C%2FP%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EApplication.ScreenUpdating%20%3D%20False%0ASet%20wsData%20%3D%20Worksheets(%22Sheet1%22)%20%20%20'Sheet%20with%20data%0Ax%20%3D%20wsData.Range(%22A1%22).CurrentRegion.Value%0ASet%20dict%20%3D%20CreateObject(%22Scripting.Dictionary%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3EIs%20it%20possible%20to%20instead%20to%20write%20the%20transformed%20data%20to%20a%20new%20sheet%20as%20opposed%20to%20columns%20H%20and%20I%3F%26nbsp%3B%26nbsp%3B%3C%2FLI%3E%3C%2FUL%3E%3CP%3EThank%20you%20again%20for%20your%20help!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-839348%22%20slang%3D%22en-US%22%3ERe%3A%20Condensing%20multiple%20outputs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-839348%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403617%22%20target%3D%22_blank%22%3E%40treecamel88%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20line...%3C%2FP%3E%3CP%3Ex%20%3D%20wsData.Range(%22A1%22).CurrentRegion.Value%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20assumes%20that%20the%20headers%20are%20in%20row1%20starting%20from%20A1%2C%20data%20starts%20from%20row2%20and%20there%20is%20no%20blank%20column%20or%20row%20in%20between%20the%20data%20set.%20It's%20an%20easy%20and%20convenient%20way%20to%20store%20all%20the%20data%20into%20an%20array.%3C%2FP%3E%3CP%3EBut%20if%20that's%20not%20the%20case%20in%20your%20actual%20file%2C%20you%20will%20have%20to%20explicitly%20define%20that%20which%20range%20contains%20the%20data%20which%20should%20be%20stored%20into%20an%20array%20x.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ee.g.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EDim%20LR%20%20As%20Long%0A'Assuming%20your%20data%20is%20in%20column%20G%20starting%20from%20row%202%20where%20row1%20being%20the%20header%20row%0ALR%20%3D%20wsData.Cells(Rows.Count%2C%20%22G%22).End(xlUp).Row%0Ax%20%3D%20wsData.Range(%22G1%3AH%22%20%26amp%3B%20LR).Value%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYes%2C%20instead%20of%20writing%20the%20output%20in%20column%20H%20and%20I%2C%20you%20can%20write%20the%20output%20in%20a%20new%20sheet%20and%20for%20that%2C%20please%20try%20the%20below%20code...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20getNamesAndTitles()%0ADim%20wsData%20%20As%20Worksheet%0ADim%20wsDest%20%20As%20Worksheet%0ADim%20x%20%20%20%20%20%20%20As%20Variant%0ADim%20dict%20%20%20%20As%20Object%0ADim%20str()%20%20%20As%20String%0ADim%20it%20%20%20%20%20%20As%20Variant%0ADim%20i%20%20%20%20%20%20%20As%20Long%0ADim%20dlr%20%20%20%20%20As%20Long%0ADim%20j%20%20%20%20%20%20%20As%20Long%0A%0AApplication.ScreenUpdating%20%3D%20False%0ASet%20wsData%20%3D%20Worksheets(%22Sheet1%22)%20%20%20'Sheet%20with%20data%0Ax%20%3D%20wsData.Range(%22A1%22).CurrentRegion.Value%0ASet%20dict%20%3D%20CreateObject(%22Scripting.Dictionary%22)%0A%0A%0AFor%20i%20%3D%202%20To%20UBound(x%2C%201)%0A%20%20%20%20If%20Not%20dict.exists(x(i%2C%201))%20Then%0A%20%20%20%20%20%20%20%20dict.Item(x(i%2C%201))%20%3D%20x(i%2C%202)%0A%20%20%20%20Else%0A%20%20%20%20%20%20%20%20dict.Item(x(i%2C%201))%20%3D%20dict.Item(x(i%2C%201))%20%26amp%3B%20%22_%22%20%26amp%3B%20x(i%2C%202)%0A%20%20%20%20End%20If%0ANext%20i%0A%0A'Adding%20a%20destination%20sheet%20for%20output%0ASet%20wsDest%20%3D%20Worksheets.Add(after%3A%3DwsData)%0A%0A'Writing%20data%20to%20newly%20added%20Sheet%0Ai%20%3D%202%0AFor%20Each%20it%20In%20dict.keys%0A%20%20%20%20dlr%20%3D%20wsDest.Range(%22A%22%20%26amp%3B%20Rows.Count).End(3)(2).Row%0A%20%20%20%20wsDest.Range(%22A%22%20%26amp%3B%20dlr).Value%20%3D%20it%0A%20%20%20%20str%20%3D%20Split(dict(it)%2C%20%22_%22)%0A%20%20%20%20If%20UBound(str)%20%2B%201%20%26gt%3B%20j%20Then%20j%20%3D%20UBound(str)%20%2B%201%0A%20%20%20%20wsDest.Range(%22B%22%20%26amp%3B%20dlr).Resize(%2C%20UBound(str)%20%2B%201).Value%20%3D%20str%0ANext%20it%0A%0AwsDest.Range(%22A1%22).Value%20%3D%20%22Name%22%0AwsDest.Range(%22B1%22).Resize(%2C%20j).Value%20%3D%20%22Title%22%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
treecamel88
New Contributor

I have column A with names that may repeat.

I have column B with a title related to the names in column A that can appear multiple times.

I want to be able to have multiples of the same name (from column A) collapsed into one row with the multiple titles (column B) appearing in multiple columns next to the names, with one title per column.

 

Is there any way to do this?  I've attached an example of what I would like to happen.

3 Replies

@treecamel88 

If you are open to a VBA solution, please try the following macro which will transform the data into the desired format.

In the attached, click the button called "Transform Data" on Sheet1 to get the data in the desired format.

 

Sub getNamesAndTitles()
Dim wsData  As Worksheet
Dim x       As Variant
Dim dict    As Object
Dim str()   As String
Dim it      As Variant
Dim i       As Long
Dim dlr     As Long
Dim j       As Long

Application.ScreenUpdating = False
Set wsData = Worksheets("Sheet1")   'Sheet with data
x = wsData.Range("A1").CurrentRegion.Value
Set dict = CreateObject("Scripting.Dictionary")


For i = 2 To UBound(x, 1)
    If Not dict.exists(x(i, 1)) Then
        dict.Item(x(i, 1)) = x(i, 2)
    Else
        dict.Item(x(i, 1)) = dict.Item(x(i, 1)) & "_" & x(i, 2)
    End If
Next i

'Clearing the data from columns H:J before writing the transformed data
'If you want to replace the original data with the transformed data, replace H:J with A:B
wsData.Columns("H:J").ClearContents

i = 2
For Each it In dict.keys
    dlr = wsData.Range("H" & Rows.Count).End(3)(2).Row
    wsData.Range("H" & dlr).Value = it
    str = Split(dict(it), "_")
    If UBound(str) + 1 > j Then j = UBound(str) + 1
    wsData.Range("I" & dlr).Resize(, UBound(str) + 1).Value = str
Next it

'Writing the transformed data in column H and adjacent columns
'To write data in column A and it's adjacent columns, replace H1 with A1 and I1 with B1
wsData.Range("H1").Value = "Name"
wsData.Range("I1").Resize(, j).Value = "Title"

Application.ScreenUpdating = True
End Sub

@Subodh_Tiwari_sktneer Thank you for your response!

In attempting to understand your response and to be able to apply it to my actual problem, I have a couple of questions:

  1. In this section, where x = wsData.Range("A1").CurrentRegion.Value, is A1 where I can change where my input "name" column begins?

 

Application.ScreenUpdating = False
Set wsData = Worksheets("Sheet1")   'Sheet with data
x = wsData.Range("A1").CurrentRegion.Value
Set dict = CreateObject("Scripting.Dictionary")

 

  • Is it possible to instead to write the transformed data to a new sheet as opposed to columns H and I?  

Thank you again for your help!

@treecamel88 

In the line...

x = wsData.Range("A1").CurrentRegion.Value

 

The code assumes that the headers are in row1 starting from A1, data starts from row2 and there is no blank column or row in between the data set. It's an easy and convenient way to store all the data into an array.

But if that's not the case in your actual file, you will have to explicitly define that which range contains the data which should be stored into an array x.

 

e.g.

Dim LR  As Long
'Assuming your data is in column G starting from row 2 where row1 being the header row
LR = wsData.Cells(Rows.Count, "G").End(xlUp).Row
x = wsData.Range("G1:H" & LR).Value

 

Yes, instead of writing the output in column H and I, you can write the output in a new sheet and for that, please try the below code...

 

Sub getNamesAndTitles()
Dim wsData  As Worksheet
Dim wsDest  As Worksheet
Dim x       As Variant
Dim dict    As Object
Dim str()   As String
Dim it      As Variant
Dim i       As Long
Dim dlr     As Long
Dim j       As Long

Application.ScreenUpdating = False
Set wsData = Worksheets("Sheet1")   'Sheet with data
x = wsData.Range("A1").CurrentRegion.Value
Set dict = CreateObject("Scripting.Dictionary")


For i = 2 To UBound(x, 1)
    If Not dict.exists(x(i, 1)) Then
        dict.Item(x(i, 1)) = x(i, 2)
    Else
        dict.Item(x(i, 1)) = dict.Item(x(i, 1)) & "_" & x(i, 2)
    End If
Next i

'Adding a destination sheet for output
Set wsDest = Worksheets.Add(after:=wsData)

'Writing data to newly added Sheet
i = 2
For Each it In dict.keys
    dlr = wsDest.Range("A" & Rows.Count).End(3)(2).Row
    wsDest.Range("A" & dlr).Value = it
    str = Split(dict(it), "_")
    If UBound(str) + 1 > j Then j = UBound(str) + 1
    wsDest.Range("B" & dlr).Resize(, UBound(str) + 1).Value = str
Next it

wsDest.Range("A1").Value = "Name"
wsDest.Range("B1").Resize(, j).Value = "Title"

Application.ScreenUpdating = True
End Sub

 

 

 

 

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
22 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
13 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies
PacketMon Components are not loading in WAC 1909
HotCakeX in Windows Admin Center on
2 Replies