Hashtable là một thư viện nằm trong “Syste" />
RaoVat24h
Excel Office

Bài 14. Hashtable

Advertisement

Hashtable là một thư viện nằm trong “System.Collections” của .NET Framework. Cho phép lưu trữ dữ liệu ở dạng mảng với Key riêng duy nhất, cho phép chèn và tìm kiếm đối tượng (Item) rất nhanh, kể cả với dữ liệu có kích cỡ lớn.
Yêu cầu: Hệ thống phải cài đặt .NET Framework

1. Khai báo Hashtable


1.1. Kiểu khai báo sớm
(Không có Tooltip khi gọi Hashtable, phải thiết lập trong Tools/References)
– Trong cửa sổ VBA, Tools menu, References.
– Tìm và check vào mục “mscorlib.dll” trong cửa sổ References – VBAProject.


Khai báo trong code:

Dim HTbl As New Hashtable


1.2. Kiểu khai báo muộn
(Không có Tooltip khi gọi Hashtable, không cần thiết lập trong Tools/References).
Khai báo trong code:

Dim HTbl As Object
Set HTbl = CreateObject("System.Collections.Hashtable")


2. Các phương thức (có 5 phương thức)

2.1. Add

HTbl.Add Key, Item


Thêm một Item vào Hashtable.
Key: Bắt buộc. Key nhận kiểu dữ liệu là số hoặc chuỗi bất kỳ. Yêu cầu Key phải duy nhất trong Hashtable, nếu Key đó đã tồn tại thì xảy ra lỗi.
Item: Bắt buộc. Item nhận kiểu dữ liệu là số hoặc chuỗi bất kỳ, giá trị đơn hoặc một mảng (array).

Ví dụ:

Sub AddMethod()
    Dim HTbl As Object
    Set HTbl = CreateObject("System.Collections.Hashtable")
    'HTbl.Add Key, Item'
    'Key: number + string, Duy nhat. Gap loi khi Key da ton tai trong HTbl'
    'Item: number + string'
    HTbl.Add 10, 100
    HTbl.Add "KeyA", 200
    HTbl.Add "KeyB", "TextB"
    HTbl.Add 20, 5
    HTbl.Add "KeyC", ""
    HTbl.Add "KeyD", Array(20, 25)
End Sub


2.2. Count

HTbl.Count


Trả về số Items có trong Hashtable.
Ví dụ:

Sub CountMethod()
    Dim HTbl As Object
    Set HTbl = CreateObject("System.Collections.Hashtable")
    'HTbl.Count'
    Dim i As Long
    For i = 1 To 10
        HTbl.Add "Key" & i, i
    Next i
    MsgBox HTbl.Count '10'
End Sub


2.3. Item

HTbl.Item (Key)
'Hoặc:'
HTbl(Key)


Gọi tới Item của Hashtable theo Key chỉ định.
Nếu Key chỉ định đưa vào chưa tồn tại trong Hashtable thì không xảy ra lỗi và kết quả trả về là rỗng.
Ví dụ:

Sub ItemMethod()
    Dim HTbl As Object
    Set HTbl = CreateObject("System.Collections.Hashtable")
    'HTbl.Item(Key)'
    'HTbl(Key)'
    HTbl.Add 10, 100
    HTbl.Add "KeyA", 200
    HTbl("KeyA") = 500
    MsgBox HTbl.Item(10)        '100'
    MsgBox HTbl.Item("KeyA")    '500'
End Sub


2.4. Remove

HTbl.Remove(Key)


Xóa một Item trong Hashtable theo Key chỉ định ứng với Item đó.
Ví dụ:

Sub RemoveMethod()
    Dim HTbl As Object
    Set HTbl = CreateObject("System.Collections.Hashtable")
    'HTbl.Remove(Key)'
    Dim i As Long
    For i = 1 To 10
        HTbl.Add "Key" & i, i
    Next i
    HTbl.Remove ("Key4")
    MsgBox HTbl.Count   '9'
End Sub


2.5. ContainsKey

HTbl.ContainsKey(Key)


Kiểm tra sự tồn tại của một Key trong Hashtable. Trả về True nếu Key đó tồn tại, ngược lại trả về False.
Ví dụ:

Sub ContainsKeyMethod()
    Dim HTbl As Object
    Set HTbl = CreateObject("System.Collections.Hashtable")
    'HTbl.ContainsKey(Key)'
    HTbl.Add 10, 100
    HTbl.Add "KeyA", 200
    MsgBox HTbl.Containskey(10)     'True'
    MsgBox HTbl.Containskey("KeyA") 'True'
    MsgBox HTbl.Containskey("KeyX") 'False'
End Sub


2.6. ContainsValue

HTbl.ContainsValue(Value)


Kiểm tra sự tồn tại giá trị của Item trong Hashtable. Trả về True nếu giá trị đó đã tồn tại, ngược lại trả về False.
Ví dụ:

Sub ContainsValueMethod()
    Dim HTbl As Object
    Set HTbl = CreateObject("System.Collections.Hashtable")
    'HTbl.ContainsValue(Value)'
    HTbl.Add 10, 100
    HTbl.Add "KeyA", "TextA"
    MsgBox HTbl.ContainsValue(100)     'True'
    MsgBox HTbl.ContainsValue("TextA") 'True'
    MsgBox HTbl.ContainsValue("TextB") 'False'
End Sub


3. Ứng dụng

– Lọc loại trùng
– …

3.1. Hàm lọc loại trùng trong một cột

'// Loc loai trung mot cot
Public Function UniqueColumnHashtable(ByVal Rng As Range) As Variant
    If Rng.Count = 1 Then UniqueColumnHashtable = Rng.Value: Exit Function
    Dim HTbl As Object, i As Long, j As Long, arr(), Result(), sKey As Variant
    Set HTbl = CreateObject("System.Collections.Hashtable")
    arr = Rng.Value
    For i = LBound(arr, 1) To UBound(arr, 1)
        sKey = arr(i, 1)
        If sKey <> "" Then
            If HTbl.Containskey(sKey) = False Then
                HTbl.Add sKey, ""
                j = j + 1
                ReDim Preserve Result(1 To j)
                Result(j) = sKey
            End If
        End If
    Next i
    UniqueColumnHashtable = Result
End Function


Rate this post

DienDan.Edu.Vn

DienDan.Edu.Vn Cám ơn bạn đã quan tâm và rất vui vì bài viết đã đem lại thông tin hữu ích cho bạn.
DienDan.Edu.Vn! là một website với tiêu chí chia sẻ thông tin,... Bạn có thể nhận xét, bổ sung hay yêu cầu hướng dẫn liên quan đến bài viết. Vậy nên đề nghị các bạn cũng không quảng cáo trong comment này ngoại trừ trong chính phần tên của bạn.
Cám ơn.

Đăng bình luận

(+84) (901) 369.468