I have a large table with lots of data, but what I'm looking at is six columns of that table - the names of people who went on a particular job together. Something like this:
+-------+--------+--------+-------+--------+-------+
| Name1 | Name2 | Name3 | Name4 | Name5 | Name6 |
+-------+--------+--------+-------+--------+-------+
| Rod | Jane | | | | |
| Jane | Freddy | Peter | Paul | | |
| Paul | | | | | |
| Mary | Jane | Rod | Peter | Freddy | Paul |
| Paul | Rod | Freddy | | | |
+-------+--------+--------+-------+--------+-------+
And what I want to end up with is this (on a different sheet):
+--------+
| Name |
+--------+
| Rod |
| Jane |
| Freddy |
| Peter |
| Paul |
| Mary |
+--------+
I want to be able to identify all the unique entries from those six columns, and then have them populate onto a different sheet. My first thought was to do it with formulae, and that worked (I used INDEX MATCH with a COUNTIF in the MATCH section), but there are 11000ish records in the table and 1200ish different names that could potentially be involved, and it was taking most of the day to process. I figured, hoped, that using VBA would make it run more quickly.
I've looked at a number of possible answers. First, I went here: Populate unique values into a VBA array from Excel , and looked at brettdj's answer (because I kind of understood where it was going), ending up with the following code:
Dim X
Dim objDict As Object
Dim lngRow As Long
Sheets("Data").Select
Set objDict = CreateObject("Scripting.Dictionary")
X = Application.Transpose(Range([be2], Cells(Rows.Count, "BE").End(xlUp)))
For lngRow = 1 To UBound(X, 1)
objDict(X(lngRow)) = 1
Next
Sheets("Crew").Select
Range("A2:A" & objDict.Count) = Application.Transpose(objDict.keys)
End Sub
which worked beautifully, for one column (BE is the Name1 column in the table above - Data is the sheet where the data is stored, Crew is the sheet where I want the unique values to go). But I couldn't for the life of me figure out how to make it take values from multiple columns (BE to BJ).
I then tried this, derived from Jeremy Thompson's answer in Quicker way to get all unique values of a column in VBA? :
Sheets("Data").Select
Range("BE:BJ").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("Crew").Range("A:A"), Unique:=True
But again, I couldn't make it put the information from multiple columns into one. Third attempt, I looked at Gary's Student's answer from How to extract unique values from two columns Excel VBA and tried this:
Dim Na As Long, Nc As Long, Ne As Long
Dim i As Long
Na = Sheets("Stroke Data").Cells(Rows.Count, "BE").End(xlUp).Row
Nc = Sheets("Stroke Data").Cells(Rows.Count, "BF").End(xlUp).Row
Ne = 1
For i = 1 To Na
Cells(Ne, "E").Value = Cells(i, "A").Value
Ne = Ne + 1
Next i
For i = 1 To Na
Cells(Ne, "E").Value = Cells(i, "C").Value
Ne = Ne + 1
Next i
Sheets("Fail").Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
(only tried two columns in that one, to see if I could figure it out that way, but no)
I'm really at a loss. As you can probably see from the above, I'm flailing around wildly, and have tried to approach this from three different angles and achieved absolutely nothing. I feel there must be a way to make the first one work, if nothing else, because it nearly worked. But I don't get it.
I suppose that I could run it for four separate columns, and then have a process which combined the four into one. But even then, I'm not sure how I'd remove the duplicates which would result (as you can see in the table above, names can appear in any column).
As long as I can end up with one column with a list of unique names, and it doesn't take hours to process, I suppose I don't really mind how I get there.