Đánh giá lequocthai.com:
- Loạt các bài viết có liên quan chuyên đề VBA:
- VBA Excel Hộp thoại thông báo
- VBA Excel Biến trong VBA Excel
- VBA Excel sử dụng Scripting Dictionary
- VBA Excel Biến trong VBA Excel
- VBA Excel Workbook, worksheet
- VBA Excel Range, Cells
- VBA Excel Hàm trong Excel VBA
- VBA Excel Events, Application Object
- VBA Excel Function and Sub Excel VBA
- VBA Excel Array Mảng trong Excel VBA
- VBA Excel FileSystemObject trong Excel VBA
- VBA Excel Collection trong Excel VBA
- VBA Excel Hashtable trong Excel VBA
- VBA Excel Stack trong Excel VBA
- VBA Excel Queue trong Excel VBA
- VBA Excel SortedList Excel VBA
- VBA Excel ArrayList Excel VBA
- VBA Excel Ví dụ về Scripting Dictionary
- Sách VBA Excel 2016 power programming with vba (pdf)
Queue là một thư viện nằm trong “System.Collections.” của .NET Framework. cùng.
Đây là cấu trúc dữ liệu dựng theo hàng để thực hiện tính chất FIFO (First In – First Out / Vào trước – Ra trước).
Cho phép lưu trữ dữ liệu (items) có kích cỡ lớn, rất hữu ích trong các tình huống muốn lưu trữ các Items theo thứ tự chỉ định.
Yêu cầu: Hệ thống phải cài đặt .NET Framework
1. Khai báo Queue
1.1. Kiểu khai báo sớm
(Không có Tooltip khi gọi Queue, 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 oQueue As New Queue
1.2. Kiểu khai báo muộn
(Không có Tooltip khi gọi Queue, không cần thiết lập trong Tools/References).
Khai báo trong code:
Dim oQueue As Object
Set oQueue = CreateObject("System.Collections.Queue")
2. Các phương thức, thuộc tính
2.1. Count Property
oQueue.Count
Trả về số Items có trong Queue.
Ví dụ:
Sub CountProperty()
Dim oQueue As Object
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.Count'
oQueue.Enqueue 5
oQueue.Enqueue "TextA"
MsgBox oQueue.Count '2'
End Sub
2.2. Enqueue
oQueue.Enqueue Item
Thêm một Item vào vị trí cuối cùng (end) của Queue.
Item nhận kiểu dữ liệu bất kỳ (kiểu số hoặc chuỗi), giá trị đơn hoặc một mảng (array).
Ví dụ:
Sub EnqueueMethod()
Dim oQueue As Object
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.Enqueue Item'
oQueue.Enqueue 5
oQueue.Enqueue "TextA"
oQueue.Enqueue ""
oQueue.Enqueue Array(20, 40)
End Sub
2.3. Peek
oQueue.Peek
Trả về Item đầu tiên của Queue và không xóa Item đó.
Ví dụ:
Sub PeekMethod()
Dim oQueue As Object, i As Long
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.Peek'
For i = 1 To 10
oQueue.Enqueue "Value-" & i
Next i
MsgBox oQueue.Peek 'Value-1'
End Sub
2.4. Dequeue
oQueue.Dequeue
Xóa và trả về Item đầu tiên của Queue.
Ví dụ:
Sub DequeueMethod()
Dim oQueue As Object, i As Long, sValue As String
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.Dequeue'
For i = 1 To 10
oQueue.Enqueue "Value-" & i
Next i
sValue = oQueue.Dequeue
MsgBox sValue 'Value-1'
MsgBox oQueue.Count '9'
End Sub
2.5. Contains
oQueue.Contains
Kiểm tra sự tồn tại của một Item trong Queue. Trả về True nếu Item đó tồn tại, ngược lại trả về False.
Ví dụ:
Sub ContainsMethod()
Dim oQueue As Object
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.Contains'
oQueue.Enqueue 5
oQueue.Enqueue "TextA"
MsgBox oQueue.Contains(5) 'True'
MsgBox oQueue.Contains("TextA") 'True'
MsgBox oQueue.Contains("TextB") 'False
End Sub
2.6. ToArray
oQueue.ToArray
Sao chép các Item trong Queue vào một mảng (Array). Mảng trả về là mảng một chiều, chỉ số cận dưới của mảng luôn băng 0, cho dù thiết lập Option Base 1.
Ví dụ:
Sub ToArrayMethod()
Dim oQueue As Object, i As Long, arr()
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.ToArray Mang 1 chieu, khong phu thuoc Option Base 1'
For i = 1 To 10
oQueue.Enqueue "Value-" & i
Next i
arr = oQueue.ToArray
MsgBox arr(0) 'Value-1'
End Sub
2.7. ToString
oQueue.ToString
Trả về tên đối tượng hiện hành, tức là “System.Collections.Queue”.
Ví dụ:
Sub ToStringMethod()
Dim oQueue As Object, sName As String
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.ToString'
sName = oQueue.ToString
MsgBox sName 'System.Collections.Queue'
End Sub
2.8. Clear
oQueue.Clear
Xóa tất cả các Items có trong Queue.
Ví dụ:
Sub ClearMethod()
Dim oQueue As Object, i As Long
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.Clear'
For i = 1 To 10
oQueue.Enqueue i
Next i
oQueue.Clear
MsgBox oQueue.Count '0'
End Sub
2.9. Clone
oQueue.Clone
Sao chép toàn bộ Queue đã dựng sang một Queue mới.
Ví dụ:
Sub CloneMethod()
Dim oQueue As Object, newQueue As Object
Set oQueue = CreateObject("System.Collections.Queue")
'oQueue.Clone'
oQueue.Enqueue 20
Set newQueue = oQueue.Clone
MsgBox newQueue.Peek '20'
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 UniqueColumnQueue(ByVal Rng As Range) As Variant
If Rng.Count = 1 Then UniqueColumnQueue = Rng.Value: Exit Function
Dim oQueue As Object, i As Long, j As Long, arr(), Result(), sKey As Variant
Set oQueue = CreateObject("System.Collections.Queue")
arr = Rng.Value
For i = LBound(arr, 1) To UBound(arr, 1)
sKey = arr(i, 1)
If sKey <> "" Then
If oQueue.Contains(sKey) = False Then
oQueue.Enqueue sKey
j = j + 1
ReDim Preserve Result(1 To j)
Result(j) = sKey
End If
End If
Next i
UniqueColumnQueue = Result
End Function
Tải file ví dụ: Queue