Extract unique values from one column using VBA

boldcode

Active Member
Joined
Mar 12, 2010
Messages
347
Hi,

I want to extract all the unique values from column A starting with A2 to the last cell of column A that has a value and copy those values into cell B2 all the way down to whatever the last cell of column B is.

I have row titles in cells A1 and B1.

Example:

Data Before Macro:
Column AColumn B
Row 1All CodesDistinct Codes
Row 2456
Row 3456
Row 4678
Row 5678
Row 6890
Row 7543
Row 8543
Row 9234
Row 10213
Row 11905
Row 12905

<colgroup><col style="mso-width-source:userset;mso-width-alt:4096;width:84pt" width="112"> <col style="mso-width-source:userset;mso-width-alt:5997;width:123pt" width="164"> <col style="mso-width-source:userset;mso-width-alt:4973;width:102pt" width="136"> </colgroup><tbody>
</tbody>



Data After Macro:

Column AColumn B
Row 1All CodesDistinct Codes
Row 2456456
Row 3456678
Row 4678890
Row 5678543
Row 6890234
Row 7543213
Row 8543905
Row 9234
Row 10213
Row 11905
Row 12905

<colgroup><col style="mso-width-source:userset;mso-width-alt:4864;width:100pt" width="133"> <col style="mso-width-source:userset;mso-width-alt:4571;width:94pt" width="125"> <col style="mso-width-source:userset;mso-width-alt:5120;width:105pt" width="140"> </colgroup><tbody>
</tbody>

Thanks,

BC
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi,

Apologies, here is a better explanation.

I am using the Sub GetUniques() that you created to go through a table (example below) and get all the unique names.

Excel 2010
OP
1Agency CandidateMonth
35Name1December
36Name1January
37Name1February
38Name1March
39Name2January
40Name2February
41Name3March
42Name3January
43Name3February

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



Your GetUniques() Sub is doing exactly what it should and providing me with a list:
Name1
Name2
Name3

I was hoping to add to your code (if i was able) to produce something like:
Excel 2010
FG
17Agency CandidateMonth
18Name1February
19Name1March
20Name2December
21Name3December
22Name3January

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



So I would like the code to look up unique values based on 2 columns not 1.

Many thanks for your help.
 
Upvote 0
So I would like the code to look up unique values based on 2 columns not 1.

Englishboxer,

Here is a macro solution for you to consider that is based on the above quote.

My results do not match yours.

Could you explain in detail how you came up with your results?


Sample worksheets:


Excel 2007
BC
2Agency CandidateMonth
3Name1December
4Name1January
5Name1February
6Name1March
7Name2January
8Name2February
9Name3March
10Name3January
11Name3February
12
Instructions



Excel 2007
FG
17Agency CandidateMonth
18
19
20
21
22
23
24
25
26
27
Sheet3


And, after the macro:


Excel 2007
FG
17Agency CandidateMonth
18Name1December
19Name1January
20Name1February
21Name1March
22Name2January
23Name2February
24Name3March
25Name3January
26Name3February
27
Sheet3


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ExtractUniqueCombinations()
' hiker95, 04/17/2017, ME649576
Dim w2 As Worksheet, w3 As Worksheet
Dim r  As Long, t As String, lr As Long
Dim d As Object, a As Variant, o As Variant, c As Long, n As Long
Set w2 = Sheets("Sheet2")
Set w3 = Sheets("Sheet3")
With w2
  lr = .Cells(Rows.Count, "O").End(xlUp).Row
  a = .Range("O35:P" & lr)
  ReDim o(1 To UBound(a, 1) + 1, 1 To UBound(a, 2))
  o(1, 1) = "Agency Candidate": o(1, 2) = "Month"
End With
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
n = 1
For r = 1 To UBound(a, 1)
  t = a(r, 1) & "," & a(r, 2)     ' & "," & a(r, 3)
  If Not d.Exists(t) Then
    n = n + 1
    For c = 1 To 2
      o(n, c) = a(r, c)
    Next c
    d.Add t, n
  End If
Next r
With w3
  .Range("F17").Resize(d.Count + 1, 2) = o
  .Columns("F:G").AutoFit
  .Activate
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ExtractUniqueCombinations macro.
 
Last edited:
Upvote 0
Thanks so much for this hiker95,

I wouldn't expect your results to match as I noticed the following day that I highlighted the wrong section when uploading the examples.

I'm sure this will be better than the workaround (c&p to another sheet and dedupe then c&p back in to the table) that I came up with.
 
Upvote 0
Thanks so much for this hiker95,

I wouldn't expect your results to match as I noticed the following day that I highlighted the wrong section when uploading the examples.

I'm sure this will be better than the workaround (c&p to another sheet and dedupe then c&p back in to the table) that I came up with.

Englishboxer,

So that we can get it right on the next try, can we have the correct raw data worksheet screenshots, and, the resulting screenshot (manually formatted by you) for the results that you are looking for?
 
Upvote 0
Englishboxer,

Here is a macro solution for you to consider that is based on the above quote.

My results do not match yours.

Could you explain in detail how you came up with your results?


Sample worksheets:

Excel 2007
BC
2Agency CandidateMonth
3Name1December
4Name1January
5Name1February
6Name1March
7Name2January
8Name2February
9Name3March
10Name3January
11Name3February
12

<colgroup><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Instructions



Excel 2007
FG
17Agency CandidateMonth
18
19
20
21
22
23
24
25
26
27

<colgroup><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet3



And, after the macro:

Excel 2007
FG
17Agency CandidateMonth
18Name1December
19Name1January
20Name1February
21Name1March
22Name2January
23Name2February
24Name3March
25Name3January
26Name3February
27

<colgroup><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet3



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub ExtractUniqueCombinations()
' hiker95, 04/17/2017, ME649576
Dim w2 As Worksheet, w3 As Worksheet
Dim r  As Long, t As String, lr As Long
Dim d As Object, a As Variant, o As Variant, c As Long, n As Long
Set w2 = Sheets("Sheet2")
Set w3 = Sheets("Sheet3")
With w2
  lr = .Cells(Rows.Count, "O").End(xlUp).Row
  a = .Range("O35:P" & lr)
  ReDim o(1 To UBound(a, 1) + 1, 1 To UBound(a, 2))
  o(1, 1) = "Agency Candidate": o(1, 2) = "Month"
End With
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
n = 1
For r = 1 To UBound(a, 1)
  t = a(r, 1) & "," & a(r, 2)     ' & "," & a(r, 3)
  If Not d.Exists(t) Then
    n = n + 1
    For c = 1 To 2
      o(n, c) = a(r, c)
    Next c
    d.Add t, n
  End If
Next r
With w3
  .Range("F17").Resize(d.Count + 1, 2) = o
  .Columns("F:G").AutoFit
  .Activate
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ExtractUniqueCombinations macro.
Dear Hiker95, I have been doing my project with a lot of index match when I finally saw this wonderful short code of yours that does a lot. I have tested and used it in some of my queries ; it's just awesome.
Can I asked your help to use this with dynamic variable instead of hard coded "Agency Candidate" and "Month" as an example above: I would like to loop through my cells and have these variable to change as the codes array runs through all the rows of multiple columns and get only the unique values. very similar with what you have done above except that I need the criteria to be changing. Criteria to match F : is tea name and G is the Hours they have. thank you. reggieneo
FG
17Team NameHours
18Name14
19Name16
20Name15
21Name14
22Name27
23Name28
24Name39
25Name34
26Name36
27

<colgroup><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
 
Upvote 0
FG
17Team NameHours
18Name14
19Name16
20Name15
21Name14
22Name27
23Name28
24Name39
25Name34
26Name36
27

<colgroup><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Can you also post the expected results from that sample data?

BTW, best not to fully quote long posts as it makes the thread harder to read/navigate. If you want to quote, quote small, relevant parts only.
 
Last edited:
Upvote 0
Hi Peter, thanks for reply. After checking each row of " F "Column, I expect to have a result in "I" Column the unique values from "G" if rows in "F " Column matches "H": please see below
FG H I
Team NameHours
18Name14 Name1 4
19Name14 Name 2 7
20Name14 Name 3 9
21Name14
22Name27
23Name27
24Name39
25Name39
26Name39

<tbody>
</tbody>
 
Upvote 0
sorry the table was messed up, here I sorted it again: thanks
F
G
H I
Team Name
Hours
18
Name1
4
Name14
19
Name1
4
Name 27
20
Name1
4
Name 39
21
Name1
4
22
Name2
7
23
Name2
7
24
Name3
9
25
Name3
9
26
Name3
9

<tbody>
</tbody>
 
Upvote 0
Hi Peter, thanks for reply. After checking each row of " F "Column, I expect to have a result in "I" Column the unique values from "G" if rows in "F " Column matches "H": please see below
FG H I
Team NameHours
18Name14 Name1 4
19Name14 Name 2 7
20Name14 Name 3 9
21Name14
22Name27
23Name27
24Name39
25Name39
26Name39

<tbody>
</tbody>
I cannot be sure what is individually in columns G, H & I but that seems like different data from your original post, particularly in relation to column G.
Can you post more clearly? (My signature block below has suggestions for that.)

EDIT: OK, I've now seen your correction. I will look at it.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

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