【VBA】Remove duplicates from an array【UNIQUE Function】

You can “Remove duplicates from an array“!

array
array
array with duplicates removed
Result

Use the following Worksheet function:
 UNIQUE Function

You can use “UNIQUE Function” only in Office365.

PR

VBA Code

As an example, Remove duplicates from an array.

Option Explicit

Sub sample()
    
    Dim arr As Variant
    Dim arrUnique As Variant
    Dim item As Variant
    
    'Create array
    arr = Array("New York", "Georgia", "Ohio", "Los Angeles", "New York", "Georgia", "Ohio")
    
    'Remove duplicates from an array
    arrUnique = WorksheetFunction.Unique(arr, True)
    
    For Each item In arrUnique
        'Output to "immediate window"
        Debug.Print item
    Next
    
End Sub

Remove duplicates from an array(Line 13).
※1st argument :target array
※2nd argument:True

PR

Result

You was able to “Remove duplicates from an array“!

Result
Result
PR

FYI No.1

Also, you can “Sorting an array“!

For more information, see the following article:

PR

FYI No.2

For more information about following, see the following official online manual:

●UNIQUE Function


“Print method” of “Debug Object”

タイトルとURLをコピーしました