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

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.

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

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: Count the Order of Occurrence of Duplicates in Excel (4 Methods)

### 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: Excel VBA to Count Duplicates in a Column (A Complete Analysis)

### 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
'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 Duplicate Values in Multiple Columns in Excel (6 Ways)

## 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.

## Related Articles #### 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 