Can a macro reduce a dataset to only the unique values per column?

bkaehny

Board Regular
Joined
Jun 11, 2009
Messages
127
Hello all,

I have a data set with about 200 columns and 10,000 rows. Here is a sample:
ColA ColB ColC
Apple 1 Fruit
Apple 2 Fruit
Cuke 1 Veg
Cuke 8 Green

The result set I would like is:
ColA ColB ColC
Apple 1 Fruit
Cuke 2 Veg
8 Green

I want to do this because I'm going to take each resulting column of distinct values and use them to populate a different sheet's data validation lists. Can a macro (or some other built-in function) do this? If so, could someone please guide me in the right direction? Thanks in advance!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
try and format it better - sometimes putting borders on the cells helps - or use dots eg apple.....1......fruit

how do you get cuke 2 veg ?
 
Upvote 0
You can use remove duplicates on the Data tab
 
Upvote 0
How is this?
Apple...1...Fruit
Apple...2...Fruit
Cuke...1...Veg
Cuke...8...Green

Becomes:
Apple...1...Fruit
Cuke...2...Veg
...8...Green

In this case, the first column only has two distinct values, so the third row in the first column ends up being blank. The second and third columns both have three distinct values, so those both end up with three rows of data.
 
Upvote 0
You can use remove duplicates on the Data tab
I've thought about that, but I would have to use Remove Duplicates on each column independently, and my sheet has 200 columns in it. I'm assuming a macro could call the Remove Duplicates function and loop through each column until it's done, but I have no idea how to do that.
 
Upvote 0
How about
Code:
Sub removedupes()
    
    Dim UsdCols As Long
    Dim i As Long
    
    UsdCols = Cells(1, Columns.count).End(xlToLeft).Column
    For i = 1 To UsdCols
      Columns(i).RemoveDuplicates 1, xlYes
    Next i
End Sub
 
Upvote 0
How about
Code:
Sub removedupes()
    
    Dim UsdCols As Long
    Dim i As Long
    
    UsdCols = Cells(1, Columns.count).End(xlToLeft).Column
    For i = 1 To UsdCols
      Columns(i).RemoveDuplicates 1, xlYes
    Next i
End Sub

That works well if my data is not inside a table, but I realize I forgot to mention that my data is inside a table. It errors on the line "Columns(i).RemoveDuplicates 1, xlYes." Sorry for forgetting to mention this in my original post.
 
Last edited:
Upvote 0
In that case I suspect that you will need to convert the table to a range.
AFAIK you can't delete individual cells in a table, only entire rows
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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