Pinterest Pixel

Highlight Duplicates Using Macros In Excel

Bryan
When I first started using Excel for data analysis, one of the recurring challenges I faced was dealing with duplicate entries.
Whether I was managing mailing lists, analyzing survey responses, or reconciling financial transactions, duplicates would sneak into my spreadsheets and potentially skew my results.

Of course, Excel provides built-in tools for handling duplicates, but once I discovered the power of macros, my workflow transformed completely.

In this article, I’ll walk you through the steps I take to highlight duplicates using macros in Excel—sharing not just the how, but also the why, so you can make the process your own.

When I first started using Excel for data analysis, one of the recurring challenges I faced was dealing with duplicate entries. Whether I was managing mailing lists, analyzing survey responses, or reconciling financial transactions, duplicates would sneak into my spreadsheets and potentially skew my results. Of course, Excel provides built-in tools for handling duplicates, but once I discovered the power of macros, my workflow transformed completely. In this article, I’ll walk you through the steps I take to highlight duplicates using macros in Excel—sharing not just the how, but also the why, so you can make the process your own.

Key Takeaways:

  • Automating the process of highlighting duplicates streamlines your workflow, saving precious time, especially with large datasets. Macros can process hundreds or thousands of rows in seconds.
  • Unlike built-in tools, macros can be tailored to fit your exact criteria, whether you’re searching across multiple columns or applying custom color coding.
  • Using macros ensures your duplicate-checking process is reproducible and consistent, reducing the risk of human error and making it easy to apply the same logic across different files.
  • Gaining familiarity with Visual Basic for Applications not only helps with duplicate detection, but it also enables you to automate other repetitive Excel tasks.
  • Once you’ve created a useful macro, you can export and share it with colleagues, fostering collaboration and boosting team productivity.

 

Highlight Duplicates Using Macros In Excel | MyExcelOnline

Download excel workbookHighlight-Duplicates.xlsm

 

Understanding Macros and Their Benefits

Why Use Macros for Duplicate Highlighting?

At first, I relied heavily on Excel’s built-in conditional formatting. But as my datasets grew larger and more complex, this approach became cumbersome. Macros, on the other hand, allowed me to automate repetitive tasks, customize my actions, and ensure consistency across multiple files. Instead of clicking through menus again and again, I could run a single macro to process my data in seconds. Not only did this save me time, but it also reduced the likelihood of missing duplicates or making manual errors. The ability to record or write a macro meant I could build exactly the solution I needed—and even share it with my colleagues.

What is a Macro and How Does it Work?

A macro in Excel is essentially a series of instructions written in VBA (Visual Basic for Applications), which Excel can follow to automate tasks. By recording a macro or writing one from scratch, I can instruct Excel to execute complex sequences—such as searching for duplicates and applying highlights—without any manual intervention. When I run a macro, Excel performs each step exactly as instructed, ensuring a repeatable and reliable outcome. This is especially valuable when my data changes frequently, and I need to re-run processes quickly.

 

Highlight Duplicates using Macros

Enable Developer Tab

Here are the quick steps to enable the Developer tab in Excel.

STEP 1: Open Excel and go to the File tab (top-left corner).

Highlight Duplicates Using Macros

STEP 2: Click Options at the bottom of the left menu.

Highlight Duplicates Using Macros

STEP 3: In the Excel Options dialog box, click Customize Ribbon on the left.

Highlight Duplicates Using Macros

STEP 4: On the right, under Main Tabs, check the box labeled Developer. Click OK.

Highlight Duplicates Using Macros

You’ll now see the Developer tab appear on the Excel ribbon, usually between View and Help.

Highlight Duplicates Using Macros

Writing the Macro: Highlighting Duplicates

STEP 1: Go to Developer > Code > Visual Basic

Highlight Duplicates Using Macros In Excel | MyExcelOnline

STEP 2: Paste in your code and Select Save. Close the window afterwards.

Sub HighlightDuplicates()
Dim cells As Range Dim cell As Range
Set cells = selection
'Loop through each cell in your selected range looking for duplicates
For Each cell In cells
'Highlight with a color of your choice, if that cell has duplicate values
If WorksheetFunction.CountIf(cells, cell.Value) > 1 Then
cell.Interior.ColorIndex = 36
End If
Next cell
End Sub

Highlight Duplicates Using Macros In Excel

STEP 3: Let us test it out! Make sure to have your cells selected.

Highlight Duplicates Using Macros In Excel

Open the sheet containing the data. Go to Developer > Code > Macros

Highlight Duplicates Using Macros In Excel | MyExcelOnline

Make sure your macro is selected. Click Run.

Highlight Duplicates Using Macros In Excel

With just one click, all of the duplicate cells now have been highlighted!

 Highlight Duplicates Using Macros In Excel

Tips & Tricks

  • Before running your macro, always make a backup copy of your spreadsheet to safeguard your original data.
  • Use the VBA editor’s debugging tools to step through your macro and catch any errors before applying it to important files.
  • Consider assigning your macro to a custom button in the Excel ribbon or a keyboard shortcut for one-click access.
  • When highlighting duplicates, use subtle colors to avoid overwhelming your spreadsheet and ensure readability.
  • If you need to check for duplicates across multiple worksheets, modify your macro to loop through each sheet—this is a huge timesaver for recurring reports.

 

FAQs

Q: Can I use a macro to highlight duplicates in non-adjacent columns?

A: Yes, you can customize your macro to scan and compare data across any columns you choose, even if they are not next to each other. By specifying the relevant column ranges in your VBA code, you can instruct Excel to look for duplicates wherever they might be. This is particularly helpful when your data is spread out or when only certain fields are prone to repetition. Adjusting your macro parameters gives you ultimate control over the process.

Q: Will running a macro slow down my workbook with very large datasets?

A: Macros are generally quite fast, but with extremely large datasets (tens of thousands of rows or more), some slowdown is possible. You can optimize performance by turning off screen updating during macro execution and making sure your code only processes the necessary ranges. It’s also a good idea to close unnecessary programs while running intensive macros. For most practical purposes, however, macros remain much quicker and more reliable than manual review.

Q: What if I want to highlight only the first occurrence of a duplicate?

A: Macros can be programmed to highlight either all duplicates or only subsequent occurrences, depending on your needs. With a simple adjustment in your VBA logic, you can ensure that the first appearance remains unhighlighted, helping you identify true duplicates rather than all instances. This is particularly useful for understanding data entry patterns or cleaning up only redundant records. Experiment with your macro’s conditions to achieve the desired behavior.

Q: Is it possible to undo changes made by a macro?

A: Unlike standard Excel actions, macros do not support the Undo feature directly. To avoid accidental changes, always save a backup of your file before running any macro. Alternatively, you can write a reversal macro or use version control to keep track of changes. Careful planning and testing help minimize the risk of irreversible edits.

Q: How can I share my duplicate-highlighting macro with others?

A: You can export your VBA macro as a .bas file or simply copy and paste the code into another user’s workbook. Encourage your colleagues to enable macros and provide brief instructions for use. Sharing not only saves others time but can also inspire collaboration and further improvements to your macro. Remember to document your code well for easy understanding and adaptation.

If you like this Excel tip, please share it



Highlight Duplicates Using Macros In Excel | MyExcelOnline


Bryan

Bryan Hong is an IT Software Developer for more than 10 years and has the following certifications: Microsoft Certified Professional Developer (MCPD): Web Developer, Microsoft Certified Technology Specialist (MCTS): Windows Applications, Microsoft Certified Systems Engineer (MCSE) and Microsoft Certified Systems Administrator (MCSA).

He is also an Amazon #1 bestselling author of 4 Microsoft Excel books and a teacher of Microsoft Excel & Office at the MyExecelOnline Academy Online Course.

See also  Add Date In Header Using Macros In Excel

Star 30 Days - Full Access Star

One Dollar Trial

$1 Trial for 30 days!

Access for $1

Cancel Anytime

One Dollar Trial
  • Get FULL ACCESS to all our Excel & Office courses, bonuses, and support for just USD $1 today! Enjoy 30 days of learning and expert help.
  • You can CANCEL ANYTIME — no strings attached! Even if it’s on day 29, you won’t be charged again.
  • You'll get to keep all our downloadable Excel E-Books, Workbooks, Templates, and Cheat Sheets - yours to enjoy FOREVER!
  • Practice Workbooks
  • Certificates of Completion
  • 5 Amazing Bonuses
Satisfaction Guaranteed
Accepted paymend methods
Secure checkout

Get Video Training

Advance your Microsoft Excel & Office Skills with the MyExcelOnline Academy!

Dramatically Reduce Repetition, Stress, and Overtime!
Exponentially Increase Your Chances of a Promotion, Pay Raise or New Job!

Learn in as little as 5 minutes a day or on your schedule.

Learn More!

Share to...