Objectif:
Détecter les lignes, du premier fichier txt importé, qui sont absentes dans le second.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionSub Comparaison() Application.ScreenUpdating = False 'Application.DisplayAlerts = False Dim nbLigneAIA As Long Dim nbLigneCRI As Long ' ------------ Compteurs de boucles - - - - - - - - - - - - Dim i As Long Dim j As Long Dim nbCol As Integer ' ------------ Booléens - - - - - - - - - - - - Dim Y As Boolean 'Dim Ys As Boolean 'Dim TabloA(), TabloN() Dim WbA As Workbook, WbN As Workbook Dim WsA As Worksheet, WsN As Worksheet Set WbA = Workbooks("Automatisation_RQT.xlsm") Set WbN = Workbooks("Automatisation_RQT.xlsm") Set WbData = Workbooks("Automatisation_RQT.xlsm") Set WsA = WbA.Worksheets("Req_AIA") Set WsN = WbN.Worksheets("Req_CRI") 'Détermination du nombre de ligne de Classeur "AIA" et "CRI" ' ILRA = WsA.Cells(65535, 1).End(xlUp).Row / Ne marche pas pour un fichier dépassant la taille max (ex: Excel 2007) ' ILRB = WsN.Cells(65535, 1).End(xlUp).Row / Ne marche pas pour un fichier dépassant la taille max (ex: Excel 2007) With Sheets("Req_AIA") nbLigneAIA = .Range("B" & .Rows.Count).End(xlUp).Row End With With Sheets("Req_CRI") nbLigneCRI = .Range("B" & .Rows.Count).End(xlUp).Row End With ' L'utilisateur choisit le nombre de colonnes à comparer nbCol = Workbooks("Automatisation_RQT.xlsm").Sheets("Donnees").Range("B1").Value + 1 ' Initialisation des booléens Y = False 'Ys = False 'Accélérateur de Macro 'Call ini_sub 'Appel à la fonction de Tri 'Call Tri_criteres 'Appel à la fonction de suppression des blancs 'Call SupprEspaces 'Détermination des absents For i = 2 To nbLigneAIA 'If IsEmpty(WsA.Cells(i, 1).Value) Then GoTo AIA Y = False For j = 2 To nbLigneCRI 'If IsEmpty(WsN.Cells(j, 1).Value) And j > ILRB Then GoTo CRI If WsA.Cells(i, 2) = WsN.Cells(j, 2) Then 'Si égalité alors on pose un drapeau Y = True WsA.Cells(i, 2).Interior.ColorIndex = 4 'et on vérifie la ligne si c'est une égalité stricte For k = 3 To nbCol ' Si égalité alors on colorie la cellule en vert If WsA.Cells(i, k) = WsN.Cells(j, k) Then WsA.Cells(i, k).Interior.ColorIndex = 4 Else 'Si la cellule en cours n'est pas déjà en vert alors on la met en orange (Eviter l'écrasement de couleur = indiquer la bonne cellule manquante) If WsA.Cells(i, k).Interior.ColorIndex <> 4 Then Ys = True 'et on colore en orange WsA.Cells(i, k).Interior.ColorIndex = 45 Y = False Exit For End If End If Next 'sinon 1ere cellule en vert 'WsN.Cells(j, 1).Interior.ColorIndex = IIf(Ys, 45, 4) 'WsA.Cells(i, 1).Interior.ColorIndex = IIf(Ys, 45, 4) 'Ys = False 'Si on trouve la ligne on sort immédiatement du 2 ieme For (éviter de parcours le reste pour rien) If Y Then Exit For End If Next ' On supprime la ligne trouvée dans les CRI avant de sortir du 2 ieme For (pour minimiser la taille de recherche) ' Et on décrémente la taille du fichier CRI If Y = True Then WsN.Cells(j, 1).EntireRow.Delete nbLigneCRI = nbLigneCRI - 1 Else 'Si pas trouvé alors on colorie en rouge WsA.Range("B" & i).Interior.ColorIndex = 3 End If Y = False Next 'AIA: MsgBox ("L'Onglet AIA est FINI") 'GoTo FIN 'CRI: MsgBox ("L'Onglet CRI est VIDE ou TERMINE ---> Fin de recherche!!!") MsgBox ("FIN DE TRAITEMENT") Set WbA = Nothing Set WbN = Nothing Set WsA = Nothing Set WsN = Nothing 'Call fin_sub End Sub
J'ai essayé avec les tableaux mais impossible de faire tourner ma macro.
Sub Comparaison() Dim nbLigneAIA As Long Dim nbLigneCRI As Long ' ------------ Compteurs de boucles - - - - - - - - - - - - Dim i As Long Dim j As Long Dim nbCol As Integer ' ------------ Booléens - - - - - - - - - - - - Dim Y As Boolean 'Dim Ys As Boolean Dim WbA As Workbook, WbN As Workbook Dim WsA As Worksheet, WsN As Worksheet Set WbA = Workbooks("Automatisation_RQT_V2.xlsm") Set WbN = Workbooks("Automatisation_RQT_V2.xlsm") Set WbData = Workbooks("Automatisation_RQT_V2.xlsm") Set WsA = WbA.Worksheets("Req_AIA") Set WsN = WbN.Worksheets("Req_CRI") 'Détermination du nombre de ligne de Classeur "AIA" et "CRI" ' ILRA = WsA.Cells(65535, 1).End(xlUp).Row / Ne marche pas pour un fichier dépassant la taille max (ex: Excel 2007) ' ILRB = WsN.Cells(65535, 1).End(xlUp).Row / Ne marche pas pour un fichier dépassant la taille max (ex: Excel 2007) With Sheets("Req_AIA") nbLigneAIA = .Range("B" & .Rows.Count).End(xlUp).Row End With With Sheets("Req_CRI") nbLigneCRI = .Range("C" & .Rows.Count).End(xlUp).Row End With 'L 'utilisateur choisit le nombre de colonnes à comparer nbCol = Workbooks("Automatisation_RQT_V2.xlsm").Sheets("Donnees").Range("B1").Value + 1 ' ------------ Tableaux - - - - - - - - - - - - Dim TabloAIA() As Variant Dim TabloCRI() As Variant ReDim TabloAIA(nbLigneAIA, nbCol) ReDim TabloCRI(nbLigneCRI, nbCol) ' Initialisation des booléens Y = False TabloAIA() = WsA.Range("A2:D" & nbLigneAIA) TabloCRI() = WsN.Range("A2:F" & nbLigneCRI) 'Détermination des absents For i = 2 To nbLigneAIA 'UBound(TabloAIA) Y = False For j = 2 To nbLigneCRI 'UBound(TabloCRI) ' Tester si la ligne n'a pas déjà été trouvée avant 'If WsN.Cells(j, 2).Value <> "" Then 'If WsA.Cells(i, 2) = WsN.Cells(j, 2) Then If TabloAIA(i - 1, 2) = TabloCRI(j - 1, 3) Then 'Si égalité alors on pose un drapeau Y = True WsA.Cells(i, 2).Interior.ColorIndex = 4 'et on vérifie la ligne si c'est une égalité stricte For k = 3 To nbCol ' Si égalité alors on colorie la cellule en vert 'If WsA.Cells(i, k) = WsN.Cells(j, k) Then If TabloAIA(i - 1, k - 1) = TabloCRI(j - 1, k) Then WsA.Cells(i, k).Interior.ColorIndex = 4 Else 'Si la cellule en cours n'est pas déjà en vert alors on la met en orange (Eviter l'écrasement de couleur = indiquer la bonne cellule manquante) If WsA.Cells(i, k).Interior.ColorIndex <> 4 Then 'Ys = True 'et on colore en orange WsA.Cells(i, k).Interior.ColorIndex = 45 Y = False Exit For End If Next End If 'End If 'Si on trouve la ligne on sort immédiatement du 2 ieme For (éviter de parcours le reste pour rien) If Y Then Exit For End If Next ' On supprime la ligne trouvée dans les CRI avant de sortir du 2 ieme For (pour minimiser la taille de recherche) ' Et on décrémente la taille du fichier CRI If Y = True Then 'WsN.Cells(j, 1).EntireRow.Delete 'TabloCRI().Rows(j - 1).Delete 'nbLigneCRI = nbLigneCRI - 1 'ReDim Preserve TabloCRI(nbLigneCRI, nbCol) 'On colorie la ligne CRI en vert WsN.Cells(j, 3).Interior.ColorIndex = 4 'On enregistre le numéro de ligne AIA sur la ligne CRI trouvée (Correspondance) WsN.Cells(j, 2).Value = i Else 'Si pas trouvé alors on colorie la ligne AIA en rouge WsA.Range("B" & i).Interior.ColorIndex = 3 End If Y = False Next Erase TabloAIA Erase TabloCRI Set WbA = Nothing Set WbN = Nothing Set WsA = Nothing Set WsN = Nothing End Sub
With Sheets("Req_AIA") nbLigneAIA = .Range("B" & .Rows.Count).End(xlUp).Row End With With Sheets("Req_CRI") nbLigneCRI = .Range("C" & .Rows.Count).End(xlUp).Row End With
Set WsA = WbA.Worksheets("Req_AIA") Set WsN = WbN.Worksheets("Req_CRI")
Sub Comparaison() Dim nbLigneAIA As Long Dim nbLigneCRI As Long ' ------------ Compteurs de boucles - - - - - - - - - - - - Dim i As Long Dim j As Long Dim nbCol As Integer ' ------------ Booléens - - - - - - - - - - - - Dim Y As Boolean Dim WbA As Workbook, WbN As Workbook Dim WsA As Worksheet, WsN As Worksheet ' ------------ Initialisation Workbook et Sheets - - - - - - - - - - - - Set WbA = Workbooks("Automatisation_RQT_V2.xlsm") Set WbN = Workbooks("Automatisation_RQT_V2.xlsm") Set WbData = Workbooks("Automatisation_RQT_V2.xlsm") Set WsA = WbA.Worksheets("Req_AIA") Set WsN = WbN.Worksheets("Req_CRI") ' ------------ Détermination des tailles des 2 fichiers - - - - - - - - - - - - With Sheets("Req_AIA") nbLigneAIA = .Range("B" & .Rows.Count).End(xlUp).Row End With With Sheets("Req_CRI") nbLigneCRI = .Range("B" & .Rows.Count).End(xlUp).Row End With 'L 'utilisateur choisit le nombre de colonnes à comparer nbCol = Workbooks("Automatisation_RQT_V2.xlsm").Sheets("Donnees").Range("B1").Value + 1 ' ------------ Tableaux - - - - - - - - - - - - Dim TabloAIA() As Variant Dim TabloCRI() As Variant ' Initialisation des booléens Y = False TabloAIA() = WsA.Range("B2:Q" & nbLigneAIA) TabloCRI() = WsN.Range("B2:Q" & nbLigneCRI) 'TabloAIA() = WsA.Range(Cells(1, 1), Cells(nbLigneAIA, nbCol + 1)) 'TabloCRI() = WsN.Range(Cells(1, 1), Cells(nbLigneCRI, nbCol + 1)) 'Détermination des absents For i = 2 To nbLigneAIA Y = False For j = 2 To nbLigneCRI ' Tester si la ligne n'a pas déjà été trouvée avant If WsN.Cells(j, nbCol + 1) <> "Trouvé" Then If TabloAIA(i - 1, 1) = TabloCRI(j - 1, 1) Then 'Si égalité alors on pose un drapeau Y = True WsA.Cells(i, 2).Interior.ColorIndex = 4 'et on vérifie la ligne si c'est une égalité stricte For k = 3 To nbCol ' Si égalité alors on colorie la cellule en vert 'If WsA.Cells(i, k) = WsN.Cells(j, k) Then If TabloAIA(i - 1, k - 1) = TabloCRI(j - 1, k - 1) Then WsA.Cells(i, k).Interior.ColorIndex = 4 Else 'Si la cellule en cours n'est pas déjà en vert alors on la met en orange (Eviter l'écrasement de couleur = indiquer la bonne cellule manquante) If WsA.Cells(i, k).Interior.ColorIndex <> 4 Then 'Ys = True 'et on colore en orange WsA.Cells(i, k).Interior.ColorIndex = 45 Y = False Exit For End If End If Next End If 'Si on trouve la ligne on sort immédiatement du 2 ieme For (éviter de parcours le reste pour rien) If Y Then Exit For End If Next If Y = True Then 'Marquer dans le fichier CRI les lignes trouvées pour éviter de laisser passer les doublons WsN.Cells(j, nbCol + 1) = "Trouvé" Else 'Si pas trouvé alors on colorie la ligne AIA en rouge WsA.Range("B" & i).Interior.ColorIndex = 3 End If Y = False Next Erase TabloAIA Erase TabloCRI Set WbA = Nothing Set WbN = Nothing Set WsA = Nothing Set WsN = Nothing End Sub