Bonjour,
J'ai besoin de votre aide !
Je souhaite copier l'onglet "B" de la feuille "LAGON" vers l'onglet "J" de la feuille "INSEE" si correspondance entre la colonne A des deux feuilles (colonne A=id).
En fait, une rechercheV n'est pas compatible car il y a des villes en doublons (donc la rechercheV copie) recopie la même ville sur des code communes différents.
Je joins un fichier et un code que j'ai commencé à utiliser!
Je vous remercie !
Sub rechercheV()
Dim WS1 As Worksheet, WS2 As Worksheet
Dim i As Long, j As Long, ligne As Long, nL1 As Long, nL2 As Long, flag As Long
Dim Chaine1 As String, chaine2 As String, table() As String
Set WS1 = Sheets("INSEE")
nL1 = WS1.Cells(Rows.Count, "A").End(xlUp).Row
ligne = nL1
Set WS2 = Sheets("LAGON")
nL2 = WS2.Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For j = 2 To nL2 ' On scanne la feuille ligne à ligne
chaine2 = WS2.Cells(j, 1) '& "|" & WS2.Cells(j, 2) & "|" & WS2.Cells(j, 3) & "|" & WS2.Cells(j, 4) & "|" & WS2.Cells(j, 5) & "|" & WS2.Cells(j, 6)
flag = 0
For i = 2 To nL1
Chaine1 = WS1.Cells(i, 1) '& "|" & WS1.Cells(i, 2) '& "|" & WS1.Cells(i, 3) & "|" & WS1.Cells(i, 4) & "|" & WS1.Cells(i, 5) & "|" & WS1.Cells(i, 6)
If chaine2 = Chaine1 Then
flag = i
WS1.Cells(flag, 10) = WS2.Cells(j, 2)
Exit For
End If
Next i
'If flag = 0 Then ' Ligne orpheline
' chaine2 = chaine2 & "|" & WS2.Cells(j, 7)
' table = Split(chaine2, "|")
' ligne = ligne + 1
' WS1.Cells(ligne, 1) = table(0)
' WS1.Cells(ligne, 2) = table(1)
' WS1.Cells(ligne, 3) = table(2)
' WS1.Cells(ligne, 4) = table(3)
' WS1.Cells(ligne, 5) = table(4)
' WS1.Cells(ligne, 6) = table(5)
' WS1.Cells(ligne, 8) = table(6)
' End If
Next j
Application.ScreenUpdating = True
End Sub