Created
May 7, 2018 02:05
-
-
Save TGDUY/3d4eb326ce4c1cf58b6af360dd0e725a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub TrangHoang() | |
Dim Dic As Object, KQ As Range | |
Dim TmpVao As Variant, TmpRa As Variant, Tmp As Variant, I As Long, J As Long | |
Set Dic = CreateObject("Scripting.Dictionary") 'Khởi tạo dictionary | |
With Sheet1 | |
TmpRa = .Range("A3:A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value 'Dữ liệu sẵn có ở cột A | |
TmpVao = .Range("G3:G" & .Cells(.Rows.Count, "G").End(xlUp).Row).Value ' Dữ liệu ở Cột G | |
Set KQ = .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row).Offset(1)' Ô cần gắn kết quả | |
End With | |
For I = LBound(TmpRa, 1) To UBound(TmpRa, 1)' Vòng lặp chạy qua cột A | |
If Not Dic.exists(CStr(TmpRa(I, 1))) Then Dic.Add CStr(TmpRa(I, 1)), I' Add những phần tử không trùng vào Dic | |
Next | |
ReDim Tmp(0)' Tạo mảng một chiều mới để lưu kết quả sau khi đối chiếu | |
J = -1 ' Do mảng một chiều bắt đầu từ phần tử 0 nên J = -1 | |
For I = LBound(TmpVao, 1) To UBound(TmpVao, 1) ' Vòng lặp chạy qua cột G | |
If Not Dic.exists(CStr(TmpVao(I, 1))) Then ' Nếu mã chưa tồn tại trong Dic | |
J = J + 1' Đây là lý do tại sao J = -1 ở dòng trên | |
ReDim Preserve Tmp(J)' Mở rộng mảng và không làm mất dữ liệu cũ | |
Tmp(J) = TmpVao(I, 1)'Đưa mã đã có trong cột G mà chưa có trong cột A vào mảng kết quả | |
End If | |
Next | |
KQ.Resize(UBound(Tmp, 1) + 1, 1) = Application.Transpose(Tmp)' Dán kết quả vào bảng tính | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment