phoenix phoenix Author
Title: Lọc danh sách bằng vba
Author: phoenix
Rating 5 of 5 Des:
Làm thế nào để có thể lọc ra danh sách duy nhất từ một danh sách có các giá trị trùng lắp với nhau.<br /> Dưới đây là code vba để lọc...
Làm thế nào để có thể lọc ra danh sách duy nhất từ một danh sách có các giá trị trùng lắp với nhau.<br />


Dưới đây là code vba để lọc ra danh sách đó.





Sub LocDuLieu_Duynhat()



    Dim vaData As Variant

    Dim colUnique As Collection

    Dim aOutput() As Variant

    Dim i As Long

    Dim LastRow As Long

    LastRow = Worksheets(Sheet1.Name).Cells(2, "A").End(xlDown).Row


    'Lay du lieu can loc dua vao mang

    vaData = Sheet1.Range("A2:G" & LastRow).Value


    'Create a new collection

    Set colUnique = New Collection

    'Loop through the data

    For i = LBound(vaData, 1) To UBound(vaData, 1)

        'Collections can't have duplicate keys, so try to

        'add each item to the collection ignoring errors.

        'Only unique items will be added

        On Error Resume Next

            colUnique.Add vaData(i, 1), CStr(vaData(i, 1))

        On Error GoTo 0

    Next i


    'size an array to write out to the sheet

    ReDim aOutput(1 To colUnique.Count, 1 To 1)


    'Loop through the collection and fill the output array

    For i = 1 To colUnique.Count

        aOutput(i, 1) = colUnique.Item(i)

    Next i


    'Write the unique values to column B

    Sheet1.Range("B2").Resize(UBound(aOutput, 1), UBound(aOutput, 2)).Value = aOutput


End Sub





Advertisement

 
Top