# VBA to Count Duplicates in Range in Excel (4 Methods)

Get FREE Advanced Excel Exercises with Solutions!

When we have a large amount of data in our Excel worksheet, it is hard to understand sometimes. If we have a certain knowledge of how many duplicate values are there, then it becomes quite easier to understand the sheet later. Implementing VBA is the most effective, quickest, and safest method to run any operation in Excel. In this article, we will show you how to count duplicates in range with the VBA macro in Excel.

## VBA to Count Duplicates in Range in Excel: 4 Methods

In the upcoming sections, we will see how to count duplicates in a range with formula, count duplicates in order and count and return results in message-box in Excel VBA.

### 1. Embed VBA to Count Duplicate Values in a Range in Excel

Look at the following dataset. Column B consists of some random numbers through range B5:B15. In Cell D5, we have stored a number, 2473. We will search this number among the random numbers in range B5:B15 and store the result in Cell E5.

Letâ€™s learn how to do that with VBA macro.

Steps:

• In the beginning, press Alt + F11 on your keyboard or go to the tab Developer -> Visual Basic to open Visual Basic Editor.

• Next, in the pop-up code window, from the menu bar, click Insert -> Module.

• Now, copy the following code and paste it into the code window.
``````Sub CountDuplicates()
Â Â Â  Dim iSheet As Worksheet
Â Â Â  Set iSheet = Worksheets("Duplicates")
Â Â Â  iSheet.Range("E5") = iSheet.Application.WorksheetFunction.CountIf(iSheet.Range("B5:B15"), iSheet.Range("D5"))
End Sub``````

• Then, press F5 on your keyboard or from the menu bar select Run -> Run Sub/UserForm. You can also just click on the small Play icon in the sub-menu bar to run the macro.

Look at the following image after the VBA code execution.

As we can see in the above image, Cell E5 holds the result of the count (3) of duplicate values of range B5 to B15.

Related Content: How to Ignore Blanks and Count Duplicates in Excel

### 2. Implement VBA Macro to Count the Repetitive Values with Formula

In the previous section, we find duplicate numbers with hardcoded macro. But this time, we will see how to count duplicate numbers with a formula in a macro code.

The to-do task is the same as before, to search the number stored in Cell D5 among the random numbers in range B5:B15 and store the result in Cell E5. But this time, we will perform Excelâ€™s COUNTIF function to get the count.

Steps:

• Same way as before, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
• Then, in the code window, copy the following code and paste
``````Sub CountDuplicatesWithFormula()
Dim iSheet As Worksheet
Set iSheet = Worksheets("Formula")
'to count the number of times the value in cell "D5" is stored in range "B5:B9"
iSheet.Range("E5").Formula = "=COUNTIF(B5:B15,D5)"
End Sub``````

• Next, Run the code as shown in the above section.

Lastly, you will get the count (3) of duplicate values of range B5 to B15 in Cell E5.

### 3. Calculate Duplicates in Order with VBA Macro in Excel

This section will describe how to extract the duplicate values in a separate column according to order. We will count the duplicates from range B5 to B15 and organize them according to the order in Column C.

Steps:

• As shown above, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
• Then, in the code window, copy the following code and pasteÂ it.
``````Sub CountDuplicatesInOrder()
Â Â Â  Dim iSheet As Worksheet
Â Â Â  'reference worksheet(change "Order" according to your sheet name)
Â Â Â  Set iSheet = Worksheets("Order")
Â Â Â Â Â Â Â  'count only positive numbers in specified range
Â Â Â Â Â Â Â  For i = 5 To 15
Â Â Â Â Â Â Â  'store the count in column C
Â Â Â Â Â Â Â  iSheet.Range("C" & i) = Application.WorksheetFunction.CountIf(iSheet.Range(iSheet.Cells(5, 2), iSheet.Cells(i, 2)), iSheet.Cells(i, 2))
Â Â Â Â Â Â Â  Next i
End Sub``````

• Now, Run the code and look at the produced result in the picture below.

Number 2473 is found at Cell B7 for the very first time. So, the next Column C shows 1 in the respective row, Cell C7. Next, the number is found again in Cell B10 for the second time, hence in Cell C10, it is displaying the duplicate count of 2. Later, the number is found in Cell B13 for the third time; as a result, Cell C13 holds the duplicate count of 3.

Related Content: How to Count Duplicate Values Only Once in Excel

### 4. Apply VBA to Count Duplicates and Show the Result in the MsgBox Dialogue Box in Excel

You can also count the duplicate values of a dataset and throw the result separately in the VBA dialogue box MsgBox. The steps to do that are shown below.

Steps:

• First, open Visual Basic Editor from the Developer tab and Insert a Module in the code window.
• Second, copy the following code and paste it into the code window.
``````Option Explicit
Sub CountDuplicatesMsgbox()
Â Â Â  Dim iHelper As Range
Â Â Â  Dim iNum As Long
Â Â Â Â Â Â Â  'reference worksheet(change "MsgBox" according to your sheet name)
Â Â Â Â Â Â Â  With Worksheets("MsgBox")
Â Â Â Â Â Â Â  'we need to set a "helper" range to store unique identifiers
Â Â Â Â Â Â Â  Set iHelper = .UsedRange.Resize(, 1).Offset(, .UsedRange.Columns.count)
Â Â Â Â Â Â Â Â Â Â Â  'reference from column B row 4 (header) to last not empty cell
Â Â Â Â Â Â Â Â Â Â Â  With .Range("B4", .Cells(.Rows.count, 1).End(xlUp))
Â Â Â Â Â Â Â Â Â Â Â  'copy unique identifiers to defined "helper" range
Â Â Â  Â Â Â Â Â Â Â Â iHelper.Value = .Value
Â Â Â Â Â Â Â Â Â Â Â  'remove duplicates from copied identifiers
Â Â Â Â Â Â Â Â Â Â Â  iHelper.RemoveDuplicates Columns:=1, Header:=xlYes
Â Â Â Â Â Â Â Â Â Â Â  'count duplicates from the difference between original reference numbers and unique ones
Â Â Â Â Â Â Â Â Â Â Â  iNum = .SpecialCells(xlCellTypeConstants).count - iHelper.SpecialCells(xlCellTypeConstants).count
Â Â Â Â Â Â Â Â Â Â Â  End With
Â Â Â Â Â  Â Â 'clear defined "helper" range
Â Â Â Â Â Â Â  iHelper.ClearContents
Â Â Â Â Â Â Â  End With
Â Â Â  MsgBox "There are " & iNum & " duplicate numbers"
End Sub``````

• Next, Run this piece of code.

There will be a pop-up dialogue box showing you the message of the total count of duplicate values of a range in a dataset of Excel.

Read More: How to Count Repeated Words in Excel

## Conclusion

In conclusion, this article showed you 4 effective methods on how to count duplicates in range with the VBA macro in Excel. I hope this article has been very beneficial to you. Feel free to ask if you have any questions regarding the topic.

## What is ExcelDemy?

ExcelDemy Learn Excel & Excel Solutions Center provides free Excel tutorials, free support , and premium Excel consultancy services for Excel and business users. Feel free to contact us with your Excel projects.
Sanjida Ahmed

Hello World! This is Sanjida, an Engineer who is passionate about researching real-world problems and inventing solutions that havenâ€™t been discovered yet. Here, I try to deliver the results with explanations of Excel-related problems, where most of my interpretations will be provided to you in the form of Visual Basic for Applications (VBA) programming language. Being a programmer and a constant solution seeker, made me interested in assisting the world with top-notch innovations and evaluations of data analysis.

We will be happy to hear your thoughts

Advanced Excel Exercises with Solutions PDF