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
Your code is now ready to run.
- 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
Your code is now ready to run.
- 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.
Read More: How to Use COUNTIF Formula to Find Duplicates
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
Your code is now ready to run.
- 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
Your code is now ready to run.
- 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
Download Workbook
You can download the free practice Excel workbook from here.
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.