Sub ABC()
Dim cn As Object, rs As Object
Dim sFile, oFile, sh As Worksheet, S, fRow&, eRow&
Dim sArr, Res(), i&, sR&, tmp$
Application.ScreenUpdating = False
Set sh = Sheets("A")
eRow = sh.Range("B" & Rows.Count).End(xlUp).Row 'Dong cuoi, Xoa du lieu cu
If eRow > 1 Then sh.Range("A2:C" & eRow).Clear 'Xoa du lieu cu
sFile = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls*),*.xlsx", _
Title:="Select File", MultiSelect:=True)
If VarType(sFile) = vbBoolean Then MsgBox ("Chua chon File du lieu"): Exit Sub
Set cn = CreateObject("adodb.connection")
For Each oFile In sFile
If Val(Application.Version) < 12 Then
cn.Open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & oFile & ";Extended Properties=""Excel 8.0;HDR=No"";")
Else
cn.Open ("provider=Microsoft.ACE.OLEDB.12.0;data source=" & oFile & ";mode=Read;extended properties=""Excel 12.0;hdr=no"";")
End If
fRow = sh.Range("B" & Rows.Count).End(xlUp).Row + 1
Set rs = cn.Execute("select * from [KASHITO$A11:B65000] where f2 is not null union all select * from [KENCHITAI$A12:B65000] where f2 is not null")
If Not rs.EOF() Then
sArr = rs.GetRows
sR = UBound(sArr, 2)
ReDim Res(0 To sR, 0 To 1)
For i = 0 To sR
If Len(sArr(0, i)) Then tmp = sArr(0, i)
Res(i, 0) = tmp
Res(i, 1) = sArr(1, i)
Next i
End If
rs.Close: cn.Close
sh.Range("A" & fRow).Resize(sR + 1, 2) = Res
eRow = sh.Range("B" & Rows.Count).End(xlUp).Row
S = Split(oFile, "\")
sh.Range("C" & fRow & ":C" & eRow) = Split(S(UBound(S)), ".xls")(0)
Next
Set cn = Nothing: Set rs = Nothing
Call SoSanh
Application.ScreenUpdating = True
End Sub
Private Sub SoSanh()
Dim sArr(), tArr(), Res(), i&, ik&, sR&, iKey$
With Sheets("A")
.UsedRange.Interior.ColorIndex = 0
sArr = .Range("A1", .Range("B" & Rows.Count).End(xlUp)).Value
tArr = .Range("H1", .Range("I" & Rows.Count).End(xlUp)).Value
End With
With CreateObject("scripting.dictionary")
sR = UBound(sArr)
For i = 1 To sR
iKey = sArr(i, 1) & "#" & sArr(i, 2)
.Item(iKey) = i
Next i
sR = UBound(tArr)
For i = 1 To sR
iKey = tArr(i, 1) & "#" & tArr(i, 2)
ik = .Item(iKey)
If ik > 0 Then
Sheets("A").Range("A" & ik).Resize(, 2).Interior.ColorIndex = 12
Sheets("A").Range("H" & i).Resize(, 2).Interior.ColorIndex = 12
End If
Next i
End With
End Sub