List Array

Finalfight40

Active Member
Joined
Apr 24, 2018
Messages
273
Office Version
  1. 365
Platform
  1. Windows
Hi All

I was wondering if it would be possible to create a formula to create the list like the sample below. My desired result will be in column C and assume the data starts in A1.

A11
B2
B32, 3
C44
D5
D6
D75, 6, 7

<tbody>
</tbody>

Thanks in Advance for taking the time to look.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
First, let's add a header since it makes the formula much easier. Second, the formula requires TEXTJOIN, which is currently only available in Office 365. But with those caveats, try:

ABC
1CodeValueList
2A11
3B2
4B32, 3
5C44
6D5
7D6
8D75, 6, 7

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
C2=IF(A2=A3,"",TEXTJOIN(", ",TRUE,INDEX(B:B,LOOKUP(2,1/(C$1:C1<>""),ROW(B$2:B2))):B2))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>



If you don't have TEXTJOIN, you'll likely need VBA.
 
Upvote 0
Just in Case, VBA solution.
Rich (BB code):
Sub MG11Jul38
Dim Rng As Range, Dn As Range, n As Long, k As Variant
Set Rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
    .Add Dn.Value, Dn.Offset(, 1)
Else
    Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn.Offset(, 1))
End If
Next
For Each k In .keys
    If .Item(k).Count = 1 Then
        .Item(k).Offset(, 1).Value = .Item(k)
    Else
        .Item(k)(.Item(k).Count).Offset(, 1) = _
        Join(Application.Transpose(.Item(k).Value), ",")
    End If
Next k
End With
End Sub
Regards Mick
 
Last edited by a moderator:
Upvote 0
Just in Case, VBA solution.
Code:
[COLOR="Navy"]Sub[/COLOR] MG11Jul38
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] k [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Set[/COLOR] Rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
[COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
    .Add Dn.Value, Dn.Offset(, 1)
[COLOR="Navy"]Else[/COLOR]
    [COLOR="Navy"]Set[/COLOR] .Item(Dn.Value) = Union(.Item(Dn.Value), Dn.Offset(, 1))
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]If[/COLOR] .Item(k).Count = 1 [COLOR="Navy"]Then[/COLOR]
        .Item(k).Offset(, 1).Value = .Item(k)
    [COLOR="Navy"]Else[/COLOR]
        .Item(k)(.Item(k).Count).Offset(, 1) = _
        Join(Application.Transpose(.Item(k).Value), ",")
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] k
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
For those who might be interested, here is an alternate way to write this macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub ConcatCodeValues()
  Dim LR As Long, Ar As Range
  LR = Cells(Rows.Count, "A").End(xlUp).Row
  Range("C2:C" & LR) = Evaluate("IF(A2:A" & LR & "<>A3:A" & LR + 1 & ",B2:B" & LR & ","""")")
  For Each Ar In Range("C2:C" & LR).SpecialCells(xlBlanks).Areas
    Ar(1).Offset(Ar.Count) = Join(Application.Transpose(Ar(1).Resize(Ar.Count + 1).Offset(, -1)), ", ")
  Next
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
FinalFight40, Your welcome

KVKMKMV, This may help !!
Rich (BB code):
Private Sub CommandButton1_Click()
Dim Rng As Range, Dn As Range, n As Long, k As Variant
Set Rng = Range("A1", Range("A" & Rows.Count).End(xlUp))

With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
If Not .Exists(Dn.Value) Then
    'Unique values in column"A" added to dictionary as "Key",
    'with column "B" Range Object added as "Item"
         
          '(Key)     (item)
    .Add Dn.Value, Dn.Offset(, 1)
Else
   'Where Duplicate values of column "A" uniques are found
   ' The Column "B" range Object is added to the "Items" of Column "A" value
   ' using the "Union" function
   
    Set .Item(Dn.Value) = Union(.Item(Dn.Value), Dn.Offset(, 1))
End If
Next
'As a result of the dictioary code above, each unique value in
'column "A" has its dictionary item set to the range of cells in column "B"
'that reflect that unique column "A" "Key"

'Loop through each "Key" in dictionary
For Each k In .keys
    'Nb:- .item(k)is a range of cells, could be one , could be multi cells
   
    If .Item(k).Count = 1 Then
            'If the Number of cells in the "item) of the "Key" "K" is one,
          'Then the "Item" of the "key" "K" is set in column "C" of the Row of ".item(k)"
        
         .Item(k).Offset(, 1).Value = .Item(k)
    Else
        'If the Number of cells in the "item) of the "Key" "K" is Greater that one,
         'Then the "Items" of the "key" "K" (a multi range)is conatenated togeter using the "Join" function.
         'That value is then placed in the last row (column "C") of the range of cells in the range  ".item(k)"
         'NB:- .item(k).count, is the last row of the range ".item(k0"
        
         .Item(k)(.Item(k).Count).Offset(, 1) = _
        Join(Application.Transpose(.Item(k).Value), ",")
    End If
Next k
End With
End Sub
Regards Mick
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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