Combine multiple columns of table data into a single column with duplicates removed

jeff_cp

Board Regular
Joined
May 21, 2015
Messages
84
I have multiple columns of data listing out employees from different divisions. Each column is a query and will have different numbers of employees and in some cases, the query can return a NULL employee. I need to combine these employees into a single column with the duplicates removed. Here is an example:

Division1Division2Division3Division4Division5Division6Division7Division8Division9Division10Division11Division12
BRIANDANNY RAYBOBBRIANJASONBRIANBRIANGINA
DAVEJASONDANNY RAYEDWARDDANNY RAYDANNY RAY
DANNY RAYJASONGINAGINAGINA
GINAKRISTENJASON
JASONKRISTEN
KRISTEN

I need to combine the employees in columns A thru L into a single list in column M and remove any duplicates. I also need to leave the NULL value if it exists in any of the queries. The resulting list would look like this:

Employees
BOB
BRIAN
DANNY RAY
DAVE
EDWARD
GINA
JASON
KRISTEN
NULL

I need VBA to do this as I want this to occur when I open the spreadsheet. I have each of the 12 queries set to run when I open the spreadsheet so they will be populated. I need the VBA to produce a list of unique employees that I can use in another worksheet with getpivotdata formulas to summarize some data on other worksheets.

Thanks in advance for any help with this.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I currently have a thread open trying to solve a similar problem.
Stacking multiple nonadjacent columns into one (best practice)

Anyways, I went ahead and took a stab at yours since it's a similar (sort of) issue to mine.
Let me know if you have any problems dissecting what I did. Just remember to enter this formula as an array formula.
Book1
ABCDEFGHIJKLMNO
1Division1Division2Division3Division4Division5Division6Division7Division8Division9Division10Division11Division12
2BRIANDANNY RAYBOBBRIANJASONBRIANBRIANGINABRIAN
3DAVEJASONDANNY RAYEDWARDDANNY RAYDANNY RAYDANNY RAY
4DANNY RAYJASONGINAGINAGINABOB
5GINAKRISTENJASONJASON
6JASONKRISTENGINA
7KRISTENDAVE
8EDWARD
9KRISTEN
10 
11 
12 
13 
Sheet3
Cell Formulas
RangeFormula
O2:O13O2=INDIRECT(TEXT(MIN(IF(($A$2:$L$7<>"")*(COUNTIF($O$1:O1,$A$2:$L$7)=0),ROW($2:$7)*100+COLUMN($A:$L),7^8)),"R0C00"),)&""
 
Upvote 0
You could probably also use a pivot table for this unique list as well.
That might be easier to make quickly, assuming this is something that you are doing often.
 
Upvote 0
Here's what the pivot table of this unique list could be made to look like.
Book1
Q
1Row Labels
2BOB
3BRIAN
4DANNY RAY
5DAVE
6EDWARD
7GINA
8JASON
9KRISTEN
Sheet3
 
Upvote 0
Division1Division2Division3Division4Division5Division6Division7Division8Division9Division10Division11Division12
BRIANDANNY RAYBOBBRIANJASONBRIANBRIANGINA
DAVEJASONDANNY RAYEDWARDDANNY RAYDANNY RAY
DANNY RAYJASONGINAGINAGINA
GINAKRISTENJASON
JASONKRISTEN
KRISTEN
Employees
BOB
BRIAN
DANNY RAY
DAVE
EDWARD
GINA
JASON
KRISTEN

Power Query:
let
    Source = Table.ToColumns(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]),
    C2T = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    Distinct = Table.Distinct(Table.ExpandListColumn(C2T, "Column1")),
    Filter = Table.SelectRows(Distinct, each ([Column1] <> null)),
    Sort = Table.Sort(Filter,{{"Column1", Order.Ascending}}),
    Ren = Table.RenameColumns(Sort,{{"Column1", "Employees"}})
in
    Ren
 
Upvote 0
Solution
Here's what the pivot table of this unique list could be made to look like.
Book1
Q
1Row Labels
2BOB
3BRIAN
4DANNY RAY
5DAVE
6EDWARD
7GINA
8JASON
9KRISTEN
Sheet3
Can you provide instructions on how to do this with a pivot table? I tried to figure it out but not having any luck.
 
Upvote 0
Can you provide instructions on how to do this with a pivot table? I tried to figure it out but not having any luck.
In order to save me from having to take lots of screen shots, I found this online which shows exactly how to achieve this pivot table.

But even better, you can now achieve this with a formula if you have the LET function available to you.
Check it out here.
 
Upvote 0
Formula option
Press CTRL+SHIFT+ENTER to enter array formulas.
Book1
ABCDEFGHIJKL
1Division1Division2Division3Division4Division5Division6Division7Division8Division9Division10Division11Division12
2BRIANDANNY RAYBOBBRIANJASONBRIANBRIANGINA
3DAVEJASONDANNY RAYEDWARDDANNY RAYDANNY RAY
4DANNY RAYJASONGINAGINAGINA
5GINAKRISTENJASON
6JASONKRISTEN
7KRISTEN
8
9
10
11
12Employees
13BOB
14BRIAN
15DANNY RAY
16DAVE
17EDWARD
18GINA
19JASON
20KRISTEN
21 
22 
23 
24 
25 
26 
27 
Sheet2
Cell Formulas
RangeFormula
A13:A27A13=IFERROR(INDEX($A$2:$L$7,MOD(MATCH(1,(NUMBERVALUE(TRIM(MID(SUBSTITUTE(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7)))," ",REPT(" ",99)),(ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7)))," ",""))+1))-1)*99+((ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7)))," ",""))+1)))=1),99)))+NUMBERVALUE(TRIM(MID(SUBSTITUTE(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>"")))," ",REPT(" ",99)),(ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>""))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>"")))," ",""))+1))-1)*99+((ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>""))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>"")))," ",""))+1)))=1),99))))-SUM(COUNTIF($A$2:$L$7,A$12:A12)),0)-1,ROWS($A$2:$L$7))+1,CEILING(MATCH(1,(NUMBERVALUE(TRIM(MID(SUBSTITUTE(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7)))," ",REPT(" ",99)),(ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7)))," ",""))+1))-1)*99+((ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,COUNTIF($A$2:$L$7,"<"&TRANSPOSE($A$2:$L$7)))," ",""))+1)))=1),99)))+NUMBERVALUE(TRIM(MID(SUBSTITUTE(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>"")))," ",REPT(" ",99)),(ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>""))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>"")))," ",""))+1))-1)*99+((ROW(OFFSET($A$1,,,LEN(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>""))))-LEN(SUBSTITUTE(TEXTJOIN(" ",FALSE,TRANSPOSE(--($A$2:$L$7<>"")))," ",""))+1)))=1),99))))-SUM(COUNTIF($A$2:$L$7,A$12:A12)),0),ROWS($A$2:$L$7))/ROWS($A$2:$L$7)),"")
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Hi,

VBA Code:
Sub test()
    Dim lastRow&, lastColumn&, rng As Range, kys As Variant
    lastRow = [a1].CurrentRegion.Rows.Count
    lastColumn = [a1].CurrentRegion.Columns.Count
    With CreateObject("Scripting.Dictionary")
        For Each rng In Range("a2", Cells(lastRow, lastColumn)).SpecialCells(xlCellTypeConstants)
            .Item(rng.Value) = Null
        Next
        kys = Application.Transpose(.keys)

    End With
    With Cells(1, lastColumn + 2).Resize(UBound(kys), 1)
        .Value = kys
        .Sort .Cells(1)
    End With
End Sub
 
Upvote 0
Another VBA option
VBA Code:
Sub jeffcp()
   Dim Ary As Variant
   Dim r As Long, c As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   
   With CreateObject("system.collections.arraylist")
      For r = 2 To UBound(Ary)
         For c = 1 To UBound(Ary, 2)
            If Not .contains(Ary(r, c)) And Ary(r, c) <> "" Then .Add Ary(r, c)
         Next c
      Next r
      .Sort
      Sheets("Sheet2").Range("A2").Resize(.Count).Value = Application.Transpose(.toarray)
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,470
Messages
6,124,990
Members
449,201
Latest member
Lunzwe73

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