1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| Sub vlookup()
Dim i As long, j As Long, k As Long
Dim table1 As Variant, table2 As Variant, tabR As Variant ' tableaux
Dim conca As Variant, conca2 As Variant
Dim dico As New Scripting.Dictionary ' dictionnaires cles -> valeur
'Table ou rechercher
table2 = Worksheets("Doc Buy").Range("A7:Z1000").Value ' data source
For i = LBound(table2, 1) To UBound(table2, 1) ' Pour i = 7 to 706
conca = table2(i, 1) & table2(i, 9)
If Not dico.Exists(conca) Then dico.Add conca, table2(i, 8) & "¤" & table2(i, 16) ' cle -> valeur
Next i
Set table2 = Nothing
'Table ce que l'on cherche
conca2 = Worksheets("POr file").Range("A2:R20000").Value ' ID
For k = LBound(conca2, 1) To UBound(conca2, 1)
table1 = conca2(k, 4) & conca2(k, 15)
Next k
ReDim tabR(LBound(table1, 1) To UBound(table1, 1), 1 To 2) ' x lignes et 2 colonnes, lbound(table1,1) premiere ligne .. on aurait lbound(table1,2) Pour premiere colonne
For j = LBound(table1, 1) To UBound(table1, 1)
If dico.Exists(table1(j, 1)) Then
' la cle existe dans table 2 : recherche Ok
tabR(j, 1) = Split(dico(table1(j, 1)), "¤")(0)
tabR(j, 2) = Split(dico(table1(j, 1)), "¤")(1)
End If
Next j
'Ecrire le resultat dans Excel
With Worksheets("POr file")
.Range(.Cells(2, 24), .Cells(20000, 25)) = tabR
End With
Application.ScreenUpdating = True
End Sub |