Dim Dic As Object
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sRng As Range, Res(), S As Variant, i As Long, sRow As Long
Application.EnableEvents = False
Application.ScreenUpdating = False
Set sRng = Intersect(Range("A2:A600000"), Target)
If Not sRng Is Nothing Then
If Dic Is Nothing Then Call CreateDic
If Not Dic Is Nothing Then
sRow = sRng.Rows.Count
ReDim Res(1 To sRow, 1 To 3)
For i = 1 To sRow
S = Dic.Item(sRng(i, 1).Value)
If TypeName(S) = "Variant()" Then
Res(i, 1) = S(0): Res(i, 2) = S(1): Res(i, 3) = S(2)
End If
Next i
sRng.Offset(0, 1).Resize(, 3).Value = Res
End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Private Sub CreateDic()
Dim sArr As Variant, j As Long, k As Long, iKey
On Error Resume Next
With CreateObject("ADODB.Connection")
.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\DATA_SP.xlsx;Extended Properties=""Excel 12.0;HDR=No"""
sArr = .Execute("select * from [DATA_SP$A2:D1000000] where f1 is not null").GetRows
End With
If Err.Number = 0 Then
Set Dic = CreateObject("Scripting.Dictionary")
For j = 0 To UBound(sArr, 2)
iKey = sArr(0, j)
If Len(iKey) > 0 Then
If Not Dic.exists(iKey) Then
Dic.Add iKey, Array(sArr(1, j), sArr(2, j), sArr(3, j))
End If
End If
Next
Else
MsgBox ("Khong tìm thay File du lieu")
On Error GoTo 0
End If
End Sub