Sub Vlookup()
Dim Dic As Object, Darr(), Arr(), i As Long, LastKQ As Long, LastNg As Long, Tmp
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'On Error Resume Next
LastKQ = Range("A3").CurrentRegion.Rows.Count + 2
If LastKQ < 4 Then
MsgBox ("Khong co du lieu Ma de lay ten, thoat chuong trinh"): Exit Sub
End If
Set Dic = CreateObject("scripting.dictionary")
Workbooks.Open Filename:=ThisWorkbook.Path & "\DULIEU.XLSX", ReadOnly:=True
With ActiveWorkbook.Sheets("NNT")
LastNg = .Range("A3").CurrentRegion.Rows.Count + 2
If LastNg < 4 Then
MsgBox ("Khong co du lieu nguon, thoat chuong trinh"): Exit Sub
End If
Darr = .Range("A4:B" & LastNg).Value
For i = 1 To UBound(Darr)
Tmp = Darr(i, 1)
'Ban chon mot trong 2 cách lay giá tri dau hoac cuoi, o 2 dong lenh duoi
[COLOR=#ff0000] If Not Dic.exists(Tmp) Then Dic.Add Tmp, Darr(i, 2) 'Lay giá tri dau[/COLOR]
[COLOR=#ff0000] 'Dic.Item(Tmp) = Darr(i, 2) 'Lay gia tri cuoi[/COLOR]
Next i
' du lieu nhieu, nhap them vao cot D va E
LastNg = .Range("D3").CurrentRegion.Rows.Count + 2
If LastNg >= 4 Then
Darr = .Range("D4:E" & LastNg).Value
For i = 1 To UBound(Darr)
Tmp = Darr(i, 1)
'Ban chon mot trong 2 cách lay giá tri o 2 dong lenh duoi
[COLOR=#ff0000] If Not Dic.exists(Tmp) Then Dic.Add Tmp, Darr(i, 2) 'Lay giá tri dau[/COLOR]
[COLOR=#ff0000] 'Dic.Item(Tmp) = Darr(i, 2) 'Lay gia tri cuoi[/COLOR]
Next i
End If
End With
ActiveWorkbook.Close False
Darr = Range("A4:A" & LastKQ).Value
ReDim Arr(1 To UBound(Darr), 1 To 1)
For i = 1 To UBound(Arr)
Tmp = Darr(i, 1)
If Dic.exists(Tmp) Then
Arr(i, 1) = Dic.Item(Darr(i, 1))
Else
Arr(i, 1) = "Khong Co"
End If
Next i
Range("B4").Resize(UBound(Arr)) = Arr
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub