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!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
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 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
-
Sometimes I wonder about these bots
by
Susan Bradley
3 hours, 40 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
5 hours, 39 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
6 hours, 39 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
6 hours, 4 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
2 hours, 34 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
9 hours, 21 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
9 hours, 22 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
3 hours, 15 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
17 hours, 30 minutes ago -
0Patch, where to begin
by
cassel23
11 hours, 32 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 7 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
18 hours, 53 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 15 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 6 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
17 hours, 47 minutes ago -
Installer program can’t read my registry
by
Peobody
27 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day, 4 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 11 hours ago -
False error message from eMClient
by
WSSebastian42
2 days, 2 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 11 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 13 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
44 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 16 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
2 minutes ago -
Outdated Laptop
by
jdamkeene
2 days, 22 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
3 days, 3 hours ago -
Another big Microsoft layoff
by
Charlie
3 days, 3 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
5 hours, 16 minutes ago -
May 2025 updates are out
by
Susan Bradley
6 hours, 57 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days, 9 hours ago
Recent blog posts
Key Links
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.