Many a time while working with VBA in Excel, we need to remove the duplicates from an array. In this article, I’ll show you how you can remove duplicates from an array with proper examples and illustrations.
Remove Duplicates from an Array with Excel VBA (Quick View)
Sub Remove_Duplicates_from_Array()
Dim MyArray() As Variant
MyArray = Array("A", "B", "C", "B", "B", "D", "C", "E", "F", "C", "B", "G")
Count = 0
For i = LBound(MyArray) To UBound(MyArray) - Count
    For j = LBound(MyArray) To UBound(MyArray) - Count2
        If i <> j And MyArray(i) = MyArray(j) And MyArray(i) <> "" Then
            MyArray(j) = ""
            Count = Count + 1
        End If
    Next j
Next i
For i = LBound(MyArray) To UBound(MyArray)
   If MyArray(i) = "" Then
       For j = i To UBound(MyArray) - 1
           MyArray(j) = MyArray(j + 1)
        Next j
       If i < UBound(MyArray) - Count + 1 Then
           i = i - 1
       End If
    End If
Next i
ReDim Preserve MyArray(UBound(MyArray) - Count)
Array_String = ""
For i = LBound(MyArray) To UBound(MyArray)
    Array_String = Array_String + MyArray(i) + " "
Next i
MsgBox Array_String
End Sub
Download Practice Workbook
Download this practice workbook to exercise while you are reading this article.
An Overview to Remove Duplicates from an Array with Excel VBA
Without further delay, let’s go to our main discussion today. Let’s see how we can remove the duplicates from an array with step-by-step analysis.
⧪ Step 1: Declaring the Array
First of all, we have to declare the array. Here we’ve declared it as MyArray which contains a few duplicates.
Dim MyArray() As Variant
MyArray = Array("A", "B", "C", "B", "B", "D", "C", "E", "F", "C", "B", "G")
⧪ Step 2: Counting the Number of Duplicates and Changing Them to an Empty String
Next, we’ll iterate through each element of the array with a for-loop and count the number of duplicate values. We’ll exchange each duplicate value with an empty string.
Count = 0
For i = LBound(MyArray) To UBound(MyArray) - Count
   For j = LBound(MyArray) To UBound(MyArray) - Count2
       If i <> j And MyArray(i) = MyArray(j) And MyArray(i) <> "" Then
           MyArray(j) = ""
           Count = Count + 1
       End If
   Next j
Next i
⧪ Step 3: Removing the Empty Strings (Places in Place of the Duplicates)
Next, we’ll remove the empty strings that were placed in the place of the duplicate values. Thus the duplicate values from the array will be removed.
We’ll use another for-loop to execute this.
For i = LBound(MyArray) To UBound(MyArray)
  If MyArray(i) = "" Then
      For j = i To UBound(MyArray) - 1
          MyArray(j) = MyArray(j + 1)
       Next j
      If i < UBound(MyArray) - Count + 1 Then
          i = i - 1
      End If
   End If
Next i
ReDim Preserve MyArray(UBound(MyArray) - Count)
⧪ Step 4 (Optional): Displaying the Array after Removing the Duplicates
The duplicates from the array have been removed. Now if you wish, you can display the array by converting it into a string separated by spaces.
Array_String = ""
For i = LBound(MyArray) To UBound(MyArray)
   Array_String = Array_String + MyArray(i) + " "
Next i
MsgBox Array_String
Therefore, the complete VBA code will be:
â§ VBA Code:
Sub Remove_Duplicates_from_Array()
Dim MyArray() As Variant
MyArray = Array("A", "B", "C", "B", "B", "D", "C", "E", "F", "C", "B", "G")
Count = 0
For i = LBound(MyArray) To UBound(MyArray) - Count
   For j = LBound(MyArray) To UBound(MyArray) - Count2
       If i <> j And MyArray(i) = MyArray(j) And MyArray(i) <> "" Then
           MyArray(j) = ""
           Count = Count + 1
       End If
   Next j
Next i
For i = LBound(MyArray) To UBound(MyArray)
  If MyArray(i) = "" Then
      For j = i To UBound(MyArray) - 1
          MyArray(j) = MyArray(j + 1)
       Next j
      If i < UBound(MyArray) - Count + 1 Then
          i = i - 1
      End If
   End If
Next i
ReDim Preserve MyArray(UBound(MyArray) - Count)
Array_String = ""
For i = LBound(MyArray) To UBound(MyArray)
   Array_String = Array_String + MyArray(i) + " "
Next i
MsgBox Array_String
End Sub
â§ Output:
Run the code (Obviously after changing the array according to your need). It’ll display the array after removing all the duplicates from it.
2 Suitable Examples to Remove Duplicates from an Array with Excel VBA
1. Develop a Macro to Remove Duplicates from a Range of Cells with Array in Excel VBA
Here we’ve got a data set extending over the range B2:E23 that contains the year, host country, champion, and runners-up of all the FIFA World Cups that took place from 1930 to 2018.
Now we’ll develop a Macro to remove the duplicate values from a range of this data set with VBA.
Let’s remove the duplicate values from the Host Countries and put them in cell G3.
The VBA code will be:
â§ VBA Code:
Sub Remove_Duplicates_from_Range()
Set Rng = Range("C3:C23")
Dim MyArray() As Variant
ReDim MyArray(Rng.Rows.Count - 1)
For i = LBound(MyArray) To UBound(MyArray)
   MyArray(i) = Rng.Cells(i + 1, 1)
Next i
Count = 0
For i = LBound(MyArray) To UBound(MyArray) - Count
   For j = LBound(MyArray) To UBound(MyArray) - Count2
       If i <> j And MyArray(i) = MyArray(j) And MyArray(i) <> "" Then
           MyArray(j) = ""
           Count = Count + 1
       End If
   Next j
Next i
For i = LBound(MyArray) To UBound(MyArray)
  If MyArray(i) = "" Then
      For j = i To UBound(MyArray) - 1
          MyArray(j) = MyArray(j + 1)
       Next j
      If i < UBound(MyArray) - Count + 1 Then
          i = i - 1
      End If
   End If
Next i
ReDim Preserve MyArray(UBound(MyArray) - Count)
For i = LBound(MyArray) To UBound(MyArray)
   Range("G3").Cells(i + 1, 1) = MyArray(i)
Next i
End Sub
â§ Output:
Run the code. It’ll remove the duplicate names from the host countries and put them in cell G3 of the worksheet.
â§ Extra:
I have removed the duplicate names from the host countries. Can you remove them from the champion and the runners-up countries? Try yourself.
Read More: How to ReDim Preserve 2D Array in Excel VBA (2 Easy Ways)
Similar Readings
- Calculate the Average of an Array with VBA (Macro, UDF, and UserForm)
- How to Sort Array with Excel VBA (Both Ascending and Descending Order)
- VBA to Transpose Array in Excel (3 Methods)
- How to Split a String into an Array in VBA (3 Ways)
2. Create a User-Defined Function to Remove Duplicates from a Range of Cells with Array in Excel VBA
Now, we’ll develop a User-Defined function to remove the duplicate values from a range of cells.
The VBA code will be:
â§ VBA Code:
Function Remove_Duplicates(Rng As Range)
Dim MyArray() As Variant
ReDim MyArray(Rng.Rows.Count - 1)
For i = LBound(MyArray) To UBound(MyArray)
   MyArray(i) = Rng.Cells(i + 1, 1)
Next i
Count = 0
For i = LBound(MyArray) To UBound(MyArray) - Count
   For j = LBound(MyArray) To UBound(MyArray) - Count2
       If i <> j And MyArray(i) = MyArray(j) And MyArray(i) <> "" Then
           MyArray(j) = ""
           Count = Count + 1
       End If
   Next j
Next i
For i = LBound(MyArray) To UBound(MyArray)
  If MyArray(i) = "" Then
      For j = i To UBound(MyArray) - 1
          MyArray(j) = MyArray(j + 1)
       Next j
      If i < UBound(MyArray) - Count + 1 Then
          i = i - 1
      End If
   End If
Next i
ReDim Preserve MyArray(UBound(MyArray) - Count)
Remove_Duplicates = MyArray
End Function
â§ Output:
Select any range of cells in your worksheet and enter the formula:
=Remove_Duplicates(C3:C23)
It’ll remove the duplicates from the host countries and show them in the selected cell.
Read More: How to Convert Range to Array in Excel VBA (3 Ways)
Things to Remember
- Don’t forget to press CTRL + SHIFT +ENTER unless you are in Office 365.
- You can repeat the formula for the champion and the runners-up countries also.
Conclusion
So, this is the way to remove the duplicates from an array with VBA in Excel. Do you have any questions? Feel free to ask us. And don’t forget to visit our site ExcelDemy for more posts and updates.
What does Count2 do? I can’t see a definition for that variable?
Hi VBANEWB, thanks for reaching out. Here, we declared Count variable first. So the VBA automatically accepts Count2 as a similar variable. So we didn’t need to declare it separately.