Remove Duplicates In Column B For Each Value In Column A? (VBA Or Formula)

DerekK

Board Regular
Joined
Jun 18, 2007
Messages
90
Office Version
  1. 2003 or older
Platform
  1. Windows
Good day,

I need to remove the duplicates under column B for each item under column A and I can't seem to figure out how to do it.

I'm using Excel 2007; I would prefer a VBA solution, as I have to do this on a monthly basis. However, if a formula is the best way to go, then that's just fine. Either way, I'm preparing the data for a pivot table, if that helps. I am unable to post the actual document in the interest of data protection.

Essentially, I'd like to turn this-

_ A B
1 Paris Bill
2 Paris Bill
3 Paris Mike
4 Paris Derek
5 Paris Derek
6 London Mike
7 London Bill
8 London Mike
9 London Bill
10 London Derek
11 Tokyo Derek
12 Tokyo Derek
13 Tokyo Derek


into this-

_ A B
1 Paris Bill
2 Paris Mike
3 Paris Derek
4 London Mike
5 London Bill
6 London Derek
7 Tokyo Derek

In other words, each city should have only one instance of any name that accompanies it; not all names will accompany each city.

Hope this make sense and I hope you can help!

Sincerely,



Derek
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
if this is something that you are doing vary rare than you can do this manually

select the columnon the ribbon

tab data

then you will see remove duplicates

this should work better for you


Code:
With ActiveSheet.Range("A:A")
.RemoveDuplicates Columns:=1, Header:=xlNo
End With
 
Last edited:
Upvote 0
Thank you for your reply.

Just to clarify, I don't want to remove all duplicates under column B. Using the example in my original post, I just want to remove duplicates for each city; so, for example, Bill appears twice for Paris and twice for London, whereas I would like Bill to appear once for Paris and once for London.

I hope this make sense; I apologize if I've misunderstood your solution.
 
Upvote 0
No Problem

When you manually select remove duplicate you need to untick the columns you don't want

the below code should do this

you can change the range as you desire

Code:
With ActiveSheet.Range("A:B")
.RemoveDuplicates Columns:=1, Header:=xlNo
End With



Old
BillParis
BillParis
BillParis
JackSpain
JackSpain
JackSpain
JackSpain
JackSpain
New
BillParis
JackSpain

<COLGROUP><COL style="WIDTH: 48pt" span=2 width=64><TBODY>
</TBODY>
 
Last edited:
Upvote 0
Sorry to belabor this but I tried that and it still doesn't work...

To use bewsh1987's example, it should look like this-

Old
Bill Paris
Bill Paris
Bill Paris
Bill Spain
Bill Spain
Bill Greece
Jack Paris
Jack Spain
Jack Spain
Jack Spain

New
Bill Paris
Bill Spain
Bill Greece
Jack Paris
Jack Spain
 
Upvote 0
Sorry I missed a little code

please try tested and works

Code:
With ActiveSheet.Range("A:B")
    .RemoveDuplicates Columns:=Array(1, 2), Header _
        :=xlNo
        
End With




Manual Way</SPAN>
1</SPAN>
Highlight the cell A:B</SPAN>
2</SPAN>
Select the data tab on the ribbon</SPAN>
3</SPAN>
On the 4th section there is Remove Duplicates</SPAN>
4</SPAN>
A box will appear with Column A & Column B Ticked</SPAN>
5</SPAN>
Click ok for Result</SPAN>

<TBODY>
</TBODY>
 
Last edited:
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG08Jan53
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] nRng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(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 & Dn.Offset(, 1)) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value & Dn.Offset(, 1), Dn
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]If[/COLOR] nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]Set[/COLOR] nRng = Dn
        [COLOR="Navy"]Else[/COLOR]
            [COLOR="Navy"]Set[/COLOR] nRng = Union(nRng, Dn)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]If[/COLOR] Not nRng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR] nRng.EntireRow.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
I tried bewsh1987's version, it worked just fine.

Many thanks to both of you!
 
Upvote 0
Bewsh, That Array bit is an interesting and useful addition to the "Remove Duplicates" function.
Something I was not aware of.
Regrds Mick
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

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