I know that I easily can remove duplicates based upon one field’s records, and that I can do so with respect to more than one field at one time. However, I want to remove duplicates if, for example, A1=A2 and B1=B2. In that case, I want record 1 or 2 removed. I attached a spreadsheet as an example. In the example, the duplicates would be records 2 and 4, so I want one those records removed. I think that I could do this through a couple of queries, but I’m looking for something easier. Thanks!
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Remove duplicates based on two fields
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Remove duplicates based on two fields
- This topic has 11 replies, 4 voices, and was last updated 13 years, 1 month ago.
Viewing 5 reply threadsAuthorReplies-
RetiredGeek
AskWoody_MVPApril 24, 2012 at 12:26 pm #1330147Jimmy,
This code should solve your problem.
Code:Option Explicit Sub RemoveDups() Dim lCurRow As Long Dim lTestRowOffset As Long lCurRow = 2 Do lTestRowOffset = 1 Do If Cells(lCurRow, 1).Value = Cells(lCurRow + lTestRowOffset, 1).Value And _ Cells(lCurRow, 2).Value = Cells(lCurRow + lTestRowOffset, 2).Value Then Rows(lCurRow + lTestRowOffset).EntireRow.Delete Else lTestRowOffset = lTestRowOffset + 1 End If Loop Until Cells(lCurRow + lTestRowOffset, 1).Value = "" lCurRow = lCurRow + 1 Loop Until Cells(lCurRow, 1).Value = "" End Sub
Please note that I solved the problem as indicated. If the data had been sorted first the code could be a little more efficient but as it stands you have to loop through the entire data set for each itteration to make sure you have all the duplicates. Of course if your dataset {table} isn’t too big this isn’t a real problem. :cheers:
Test Data
30689-TestData
Results
30690-Results -
WSJimmy-W
AskWoody LoungerApril 24, 2012 at 1:29 pm #1330148Thanks! If I may impose with a couple of questions, can I edit the code if the two fields are not contiguous, e.g., A2 and C2? Would it then be 2TestRowOffset? I also wonder about date/time granularity. Two times may be formatted to appear as 13:10, but really may be 13:10:45 and 13:10:55, etc. Does the code use the formatted time or the actual numeric time? (My worksheet is sorted by date/time, descending order.)
-
RetiredGeek
AskWoody_MVPApril 24, 2012 at 7:54 pm #1330181Jimmy,
1. Can I edit the code if the two fields are not contiguous, e.g., A2 and C2? Yes just change the Column Numbers, they are the hard coded numbers. e.g. A=1 B=2 C=3 thus A2 & C2 would be
Code:If Cells(lCurRow, 1).Value = Cells(lCurRow + lTestRowOffset, 1).Value And _ Cells(lCurRow, 3).Value = Cells(lCurRow + lTestRowOffset, 3).Value Then
2. Does the code use the formatted time or the actual numeric time? Actual Time
Although the code could be modified to use formatted time.Code:If Format(Cells(lCurRow, 1).Value, "hh:mm") = _ Format(Cells(lCurRow + lTestRowOffset, 1).Value, "hh:mm") And _ Cells(lCurRow, 2).Value = Cells(lCurRow + lTestRowOffset, 2).Value Then
:cheers:
-
WSRoger Govier
AskWoody Lounger
-
-
RetiredGeek
AskWoody_MVPApril 26, 2012 at 10:55 am #1330445Roger,
Nice! However the VBA is more robust since it will work if the records are sorted or not. I tried your solution on my test data and it didn’t get the right answer. Then I remembered the OP has said his data was sorted by date/time decending, my test data was not. Upon sorting your solution worked perfectly and is an interesting approach to the problem.
I notice you’re a fairly new poster here so let me take this opportunity to welcome you to the Lounge as a poster. It’s always nice to have more talent, especially MVPs, to add new perspectives to the Lounge’s talent base. :cheers:
-
WSRoger Govier
AskWoody LoungerApril 26, 2012 at 12:44 pm #1330465Hi
Many thanks for the welcome.However, I fear you are incorrect in your assertion regarding sorting.
The formula solution will work with either sorted or unsorted data, as the attached workbook shows. I added some more data to jumble the order up a bit.The formula in D2
=COUNTIF($C$2:C2,C2)>1
which becomes
=COUNTIF($C$2:C14,C14)>1
by the time it gets to D14
is not concerned with any order, or referencing the row above, it is doing a Countif on an ever expanding range to see if there is more than one occurrence of the concatenated value.
-
-
RetiredGeek
AskWoody_MVPApril 26, 2012 at 1:03 pm #1330469Roger,
I’m still getting the same results. :confused:
Here’s my test workbook with both your formula and my macro, maybe you can see what I am doing wrong. :cheers: -
WSRoger Govier
AskWoody Lounger -
WSSimon Bardby
AskWoody LoungerApril 27, 2012 at 5:53 am #1330568If you’re using Excel 2007 or greater you can use the remove duplicates function once you’ve created a key field through concatenation (so the count formula is unecessary).
FWIW it’s not necessary to delimit the fields (in your concatenated key field) as this is a reference field only, ie
In C2 enter
=A2&B2note that it can easily be extended to cover as many fields as necessary (and they don’t need to be contiguous)
and the count field should cover the entire column so that the data doesn’t have to be sorted
In D2 enter
=COUNTIF(C:C,C2) -
WSRoger Govier
AskWoody LoungerApril 27, 2012 at 11:40 am #1330579Hi Simon
You are quite right that there is a remove duplicate function in later versions, I was trying to give a generic solution.
With this particular set of data you are correct, a separator is not required in the concatenation.
However, I do it as a matter of course with all concatenations, because there are occasions when you can get tripped up with different numbers of characters in the concatenated fields.On the matter of the Countif function, however, you are totally wrong.
Your formula will count the number of occurrence in the whole data set, and that would not provide a list of uniques.
You would not be able to select the rows you wanted to delete, as it would also contain the first occurrence of the value.The formula I proposed, does not need to be sorted.
-
-
-
-
RetiredGeek
AskWoody_MVPApril 28, 2012 at 1:58 pm #1330682Just to keep you all updated Roger & I had taken our discussion offline to try to resolve why we were getting different results.
It turns out it was due to my final version of code checking the time in HH:MM format vs his solution checking in HH:MM:SS format. Roger offered the following to make his do the same thing.[noparse]=TEXT(A2,”hh:mm”)&”|”&B2[/noparse]
I’d like to take this opportunity to thank Roger for hanging with me through this and discovering the problem, and of course for his elegant solution.:clapping:
Thanks to him we all now have a new technique to add to our Excel toolbox. :cheers:
Viewing 5 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Firefox Red Panda Fun Stuff
by
Lars220
1 hour, 16 minutes ago -
How start headers and page numbers on page 3?
by
Davidhs
4 hours, 48 minutes ago -
Attack on LexisNexis Risk Solutions exposes data on 300k +
by
Nibbled To Death By Ducks
2 hours, 18 minutes ago -
Windows 11 Insider Preview build 26200.5622 released to DEV
by
joep517
13 hours, 29 minutes ago -
Windows 11 Insider Preview build 26120.4230 (24H2) released to BETA
by
joep517
13 hours, 31 minutes ago -
MS Excel 2019 Now Prompts to Back Up With OneDrive
by
lmacri
3 hours, 12 minutes ago -
Firefox 139
by
Charlie
15 hours, 53 minutes ago -
Who knows what?
by
Will Fastie
8 hours, 36 minutes ago -
My top ten underappreciated features in Office
by
Peter Deegan
14 hours, 15 minutes ago -
WAU Manager — It’s your computer, you are in charge!
by
Deanna McElveen
8 hours, 38 minutes ago -
Misbehaving devices
by
Susan Bradley
16 hours, 23 minutes ago -
.NET 8.0 Desktop Runtime (v8.0.16) – Windows x86 Installer
by
WSmeyerbos
1 day, 20 hours ago -
Neowin poll : What do you plan to do on Windows 10 EOS
by
Alex5723
1 hour, 30 minutes ago -
May 31, 2025—KB5062170 (OS Builds 22621.5415 and 22631.5415 Out-of-band
by
Alex5723
1 day, 18 hours ago -
Discover the Best AI Tools for Everything
by
Alex5723
17 hours, 57 minutes ago -
Edge Seems To Be Gaining Weight
by
bbearren
1 day, 9 hours ago -
Rufus is available from the MSFT Store
by
PL1
1 day, 17 hours ago -
Microsoft : Ending USB-C® Port Confusion
by
Alex5723
2 days, 19 hours ago -
KB5061768 update for Intel vPro processor
by
drmark
19 hours, 44 minutes ago -
Outlook 365 classic has exhausted all shared resources
by
drmark
18 hours, 26 minutes ago -
My Simple Word 2010 Macro Is Not Working
by
mbennett555
2 days, 15 hours ago -
Office gets current release
by
Susan Bradley
2 days, 18 hours ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
4 days, 8 hours ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
3 days, 16 hours ago -
Stop the OneDrive defaults
by
CWBillow
4 days, 9 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
4 days, 19 hours ago -
X Suspends Encrypted DMs
by
Alex5723
4 days, 21 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
4 days, 21 hours ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
4 days, 22 hours ago -
OpenAI model sabotages shutdown code
by
Cybertooth
4 days, 22 hours ago
Recent blog posts
Key Links
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.