Skip to content

Instantly share code, notes, and snippets.

@DuongAQ
Created March 22, 2018 04:12
Show Gist options
  • Save DuongAQ/c6352f8ef46501c70979a83e6e7808bb to your computer and use it in GitHub Desktop.
Save DuongAQ/c6352f8ef46501c70979a83e6e7808bb to your computer and use it in GitHub Desktop.
Private Sub Worksheet_Activate() 'Lấy danh sách không trùng vào ComboBox
Dim rng As Range 'Tạo biến Vùng danh sách gốc
Dim r As Range 'Tạo biến để thực hiện vòng lặp trong danh sách gốc
Dim Dic As Object 'Tạo biến để gọi đối tượng dictionary
Dim ws As Worksheet 'Tạo biến Sheet chứa danh sách gốc
Set ws = ActiveSheet 'Sheet chứa danh sách gốc là sheet đang làm việc
Set rng = ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp)) 'Vùng danh sách gốc nằm ở cột A, bắt đầu từ ô A2
Set Dic = CreateObject("scripting.dictionary") 'Thiết lập đối tượng dictionary
Dic.CompareMode = vbTextCompare 'Thực hiện việc so sánh các ký tự text trong thư viện dic
For Each r In rng
Dic(r.Value) = Empty 'Các giá trị trùng sẽ được bỏ đi
Next
With ComboBox1
.ListFillRange = "" 'Xóa danh sách cũ đã có trước đó
If .ListCount = 0 Then 'Thực hiện việc nạp danh sách mới
.List = Application.Transpose(Dic.keys) 'Lấy kết quả còn lại ở Dic vào Danh sách chọn của combobox1
.ListIndex = 0
End If
End With
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment