Excel Macro code to Highlight Duplicates from Selection

How to Highlight Duplicates from Selection in excel sheet through VBA Macro Code

To use VBA macro code in an Excel spreadsheet, you need to do the following:

  1. Open the Excel spreadsheet where you want to use the macro code.

  2. Press “Alt + F11” to open the VBA developer window.

  3. In the VBA developer window, click the “Insert” menu and select “Module” to create a new module.

  4. Paste the VBA macro code into the module window.

  5. To run the macro, click the “Run” button in the VBA developer window or press “F5” on your keyboard.

Alternatively, you can also create a button or a shortcut key to run the macro. To create a button, go to the “Developer” tab in the main Excel window and click the “Insert” button. Then select a button control and draw it on the worksheet. Right-click the button and select “Assign Macro” to assign the macro code to the button. To create a shortcut key, go to the “Macros” tab in the VBA developer window, select the macro from the list, and click the “Options” button. Then, enter a shortcut key in the “Shortcut key” field.

Note: If you are running the macro for the first time, you may need to enable macros in Excel by going to the “File” tab and selecting “Options.” Under the “Trust Center” settings, click the “Trust Center Settings” button and go to the “Macro Settings” tab. Select “Enable all macros” and click “OK.”

Copy Below VBA Macro Code for Highlight Duplicates from Selection

Highlight Duplicates from Selection

Sub HighlightDuplicateValues()
Dim myRange As Range
Dim myCell As Range
Set myRange = Selection
For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 36
End If
Next myCell
End Sub

This macro will check each cell of your selection and Highlight Duplicates from Selection.  You can also change the color from the code.

How to Highlight Duplicate Values in Excel using VBA Codes

While working on huge Excel data, duplicate values are always a concern. Most of the time I use to remove duplicate option to remove all those values.  And, I’m sure you must be doing the same

But removing these duplicate values or just counting them never sort out the problem.

The important thing is to review all the duplicate values before deletion.

Yes, that’s right.

Once you highlight all those values, you can check and then you can delete them.

In this post, we’d like to share with you 4 different VBA codes to highlight duplicate values.

And the part is, these codes can highlight cell using different ways. You can simply copy-paste them in your VBA editor and use them.

So let’s get started…

A. How to Highlight Duplicate Values Within Each Row using VBA Macros

This VBA code checks all the cells from a row and highlights all the cells which are duplicate within a row.

In simple words, if a row has the value “522” twice then it will be considered as duplicate. But if the another 522 is in another row then it will be considered as unique.

highlight duplicate values from each row using VBA code

Sub DuplicateValuesFromRow()
'Declare All Variables.
Dim myCell As Range
Dim myRow As Integer
Dim myRange As Range
Dim myCol As Integer
Dim i As Integer
'Count Number of Rows and Columns
myRow = Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Count
myCol = Range(Cells(1, 1), Cells(1, 1).End(xlToRight)).Count
'Loop Each Row To Check Duplicate Values and Highlight cells.
For i = 2 To myRow
Set myRange = Range(Cells(i, 2), Cells(i, myCol))
For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 3
End If
Next
Next
End Sub
If you go through this macro, you will find that we have used a loop to check each row for duplicate values and highlight them with a color.

Important Points

  1. Your data should not have a blank row or column in it, otherwise, it ignores that cell.
  2. Starting cell of your data should be “A1” cell. And if you want to adjust the starting point you have to adjust the code.
  3. First row and column of your data sheet should be heading.

B. How to Highlight Duplicate Values Within Each Column using Excel VBA Macro code

This VBA code checks all the cells from a column and highlights all the cells which are duplicate within each column.

In simple words, if a column has the value “231” twice then it will be considered as duplicate. But if the another “231” is in another column then it will be considered as unique.

highlight duplicate values from each column using VBA code
Sub DuplicateValuesFromColumns()
'Declare All Variables
Dim myCell As Range
Dim myRow As Integer
Dim myRange As Range
Dim myCol As Integer
Dim i As Integer
'Count number of rows & column
myRow = Range(Cells(1, 1), Cells(1, 1).End(xlDown)).Count
myCol = Range(Cells(1, 1), Cells(1, 1).End(xlToRight)).Count
'Loop each column to check duplicate values & highlight them.
For i = 2 To myRow
Set myRange = Range(Cells(2, i), Cells(myRow, i))
For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 3
End If
Next
Next
End Sub

C. How to Highlight Duplicate Values Within Selection using Excel VBA Macro code

Let’s say you just want to highlight cells with duplicate values from the selection, this code can help you in this.

To use this code you just need to select a range of cells and run this code. It checks each cell with the selection and highlights it with red color if a cell has a duplicate value.

highlight duplicate values from the selection using VBA code
Sub DuplicateValuesFromSelection()
Dim myRange As Range
Dim i As Integer
Dim j As Integer
Dim myCell As Range
Set myRange = Selection
For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 3
End If
Next
End Sub

D. How to Highlight Duplicate Values Entire Data using Excel Macro code

If you have a large data-set, you can use this macro code to check the entire data and highlight duplicate values.

This code loops through each cell one by one and applies red color all those cells which are the duplicate.

Sub DuplicateValuesFromTable()
Dim myRange As Range
Dim i As Integer
Dim j As Integer
Dim myCell As Range
Set myRange = Range("Table1")
For Each myCell In myRange
If WorksheetFunction.CountIf(myRange, myCell.Value) > 1 Then
myCell.Interior.ColorIndex = 3
End If
Next
End Sub

Important Note: In the above code, we have used the table name “Table1”, you can change this name from the code. Or, if you have a range then you can simply use range name.

[Bonus Tip] Count Duplicate Values

This code helps you to count the numbers of duplicate values from the selection. When you run this code, it returns a message box which shows the count.

Sub CountDuplicates()
Dim i As Integer
Dim j As Integer
Dim myCell As Range
Dim myRange As Integer
myRange = Range("Table1").Count
j = 0
For Each myCell In Range("Table1")
If WorksheetFunction.CountIf(Range("Table1"), myCell.Value) > 1 Then
j = j + 1
End If
Next
MsgBox j
End Sub

Again in the above code, we have used table name and you change it or replace it with a range.

In different situations, we need check duplicate values in the different ways. And, all the above codes can be helpful for you in this.

If you want you can change the highlight color from the codes using color index number.

RECENTLY UPLOADED EXCEL TEMPLATES

Insurance Quote Template

Insurance Quote

Introduction: Streamlining Insurance Quotations An insurance quote is a vital document in the insurance industry, serving as a preliminary estimate …

Download Now
Freelance Quotation Template

Freelance Quotation

Introduction: Mastering Freelance Quotations In the world of freelancing, creating an effective quotation is crucial for outlining the scope and …

Download Now
Fencing Quotation

Fencing Quotation

Introduction: Navigating Fencing Service Quotations A fencing service quotation is an essential tool for businesses in the fencing industry. It …

Download Now
Event Planning Quote

Event Planning Quotations

Introduction: Excelling with Event Planning Quotations An event planning quote is a critical document for event planners, detailing proposed services …

Download Now
Construction Quote Template

Construction Quote

Introduction: Optimizing Construction Quotations A construction quote is a fundamental document in the construction industry, serving as a formal proposal …

Download Now
Cleaning Quote

Cleaning Quote

Introduction: Perfecting Cleaning Service Quotations Creating an effective cleaning service quotation is crucial in the cleaning business. It serves as …

Download Now