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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
Sub comparer_fichiers()
Dim num_index As Integer
Dim derColonneSource As Integer, derColonneCible As Integer
Dim cpt_col_source As Integer, cpt_col_cible As Integer
Dim fichier_source As Workbook, fichier_cible As Workbook
Dim feuille_source As Worksheet, feuille_cible As Worksheet
'Déclaration du fichier et de la feuille source
Set fichier_source = Workbooks("nomDuFichierSource")
Set feuille_source = fichier_source.Worksheets("nomDeLaFeuilleSource")
'Déclaration du fichier et de la feuille cible
Set fichier_cible = Workbooks("nomDuFichierCible")
Set feuille_cible = fichier_c.Worksheets("nomDeLaFeuilleCible")
'Derniere colonne de la feuille source
derColonneSource = feuille_source.Range("A1").End(xlToLeft).Column
'Derniere colonne de la feuille cible
derColonneCible = feuille_cible.Range("A1").End(xlToLeft).Column
'Boucle sur les colonnes de la feuille source
For cpt_col_source = 1 To derColonneSource
num_index = feuille_source.Cells(cpt_col_source, 1)
'Recherche du num_index dans la feuille cible
Set R = feuille_cible.Range("A1:IV1").Find(num_index, lookat:=xlWhole)
If Not R Is Nothing Then 'Si num_index a été trouvé dans la feuille cible... Mise à jour des données
feuille_cible.Cells(4, R.Column).Value = feuille_source.Cells(4, cpt_col_source).Value 'Adresse
feuille_cible.Cells(5, R.Column).Value = feuille_source.Cells(5, cpt_col_source).Value 'Semaine
'.........
'.........
Else 'Si le num_index n'existe pas
derColonneCible = feuille_cible.Range("A1").End(xlToLeft).Column + 1
feuille_cible.Cells(1, derColonneCible) = feuille_source.Cells(1, cpt_col_source) 'num_index
feuille_cible.Cells(2, derColonneCible) = feuille_source.Cells(2, cpt_col_source) 'nom
feuille_cible.Cells(3, derColonneCible) = feuille_source.Cells(3, cpt_col_source) 'prenom
feuille_cible.Cells(4, derColonneCible) = feuille_source.Cells(4, cpt_col_source) 'Adresse
feuille_cible.Cells(5, derColonneCible) = feuille_source.Cells(5, cpt_col_source) 'Semaine
'.........
'.........
End If
Next cpt_col_source
End Sub |
Partager