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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
Option Explicit
Public Sub Comparer()
Dim wSh As Worksheet, wSh1 As Worksheet, wSh2 As Worksheet, wShM As Worksheet
Dim kR As Long, kR1 As Long, kR2 As Long, kRM As Long, kC As Long
Dim Rng As Range, Rng1 As Range, Rng2 As Range, PM1 As Long, PM2 As Long
Dim bNoDiff As Boolean, sMetier As String
Application.ScreenUpdating = False
'--- vider les feuilles Metiers (sans remettre ligne de titre)
For Each wSh In ThisWorkbook.Worksheets
Select Case wSh.Name
Case "Comparatif", "Liste 1", "Liste 2"
'--- ne rien faire
Case Else
wSh.Cells.Clear '--- vide entièrement la feuille
End Select
Next wSh
'--- intialise
Dim t As Single: t = Timer
Set wSh = ThisWorkbook.Worksheets("Comparatif")
Set wSh1 = ThisWorkbook.Worksheets("Liste 1")
Set wSh2 = ThisWorkbook.Worksheets("Liste 2")
kR = 1
'--- parourir "Comparatif"
Do
kR = kR + 1
If wSh.Cells(kR, 1) = "" Then GoTo fin '--- sortie
PM1 = wSh.Cells(kR, 1).Value
PM2 = wSh.Cells(kR, 2).Value
If Application.WorksheetFunction.CountIf(wSh1.Range("A:A"), PM1) > 1 Then
MsgBox "Il y a plusieurs fois le même PM (" & PM1 & ")" & vbLf & _
"utilisé dans la feuille " & wSh1.Name, , "Opération interrompue"
GoTo fin '--- sortie
End If
If Application.WorksheetFunction.CountIf(wSh2.Range("A:A"), PM2) > 1 Then
MsgBox "Il y a plusieurs fois le même PM (" & PM2 & ")" & vbLf & _
"utilisé dans la feuille " & wSh2.Name, , "Opération interrompue"
GoTo fin '--- sortie
End If
sMetier = wSh.Cells(kR, 3).Value
If Not Sheet_Exists(sMetier) Then
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = sMetier
Range("A1:O1") = Array("PM", "", "Libellé", "LOT", "", "", "", "AD", "AS", "EC", "DS", "GD", "DL", "MI", "NG")
Range("A1:O1").Select
With Range("A1:O1")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.Font.Bold = True
.Interior.Color = RGB(204, 204, 0)
End With
With Selection.Borders()
.LineStyle = xlContinuous
.Weight = xlThin
End With
End If
Set Rng1 = wSh1.Range("A:A").Find(wSh.Cells(kR, 1))
Set Rng2 = wSh2.Range("A:A").Find(wSh.Cells(kR, 2))
If Rng1 Is Nothing Or Rng2 Is Nothing Then
MsgBox "Ligne " & kR & " avec un n° de liste non trouvée", , "Opération interrompue"
wSh.Cells(kR, 1).Select
GoTo fin '--- sortie
End If
kR1 = Rng1.Row
kR2 = Rng2.Row
'--- détecte différence éventuelle
bNoDiff = True
For kC = 7 To 13
If wSh1.Cells(kR1, kC) <> wSh2.Cells(kR2, kC).Value Then
bNoDiff = False
Exit For
End If
Next kC
If bNoDiff = False Then '--- si différence constatée
Set wShM = ThisWorkbook.Worksheets(sMetier)
kRM = wShM.Cells(Rows.Count, 1).End(xlUp).Row
If kRM = 1 Then kRM = kRM + 1 Else: kRM = kRM + 2
'--- recopier
wSh.Cells(kR, 1).Copy wShM.Cells(kRM, 1) '--- PM
wSh.Cells(kR, 3).Copy wShM.Cells(kRM, 3) '--- Métier
wSh1.Cells(kR1, 2).Copy wShM.Cells(kRM, 4) '--- Lot
wSh1.Range(wSh1.Cells(kR1, 6), wSh1.Cells(kR1, 13)).Copy wShM.Cells(kRM, 8)
wSh.Cells(kR, 2).Copy wShM.Cells(kRM + 1, 1) '--- PM
wSh.Cells(kR, 3).Copy wShM.Cells(kRM + 1, 3) '--- Métier
wSh2.Cells(kR2, 2).Copy wShM.Cells(kRM + 1, 4) '--- Lot
wSh2.Range(wSh2.Cells(kR2, 6), wSh2.Cells(kR2, 13)).Copy wShM.Cells(kRM + 1, 8)
With wShM.Range(wShM.Cells(kRM, 1), wShM.Cells(kRM + 1, 15)).Borders
.LineStyle = xlContinuous
.Weight = xlThin
End With
'--- marquer différences
For kC = 8 To 15
If wShM.Cells(kRM, kC) <> wShM.Cells(kRM + 1, kC) Then
If wShM.Cells(kRM, kC) = "" Then
wShM.Cells(kRM, kC).Interior.Color = 3394611 '--- vert
wShM.Cells(kRM, kC).Value = wShM.Cells(kRM + 1, kC).Value
ElseIf wShM.Cells(kRM + 1, kC) = "" Then
wShM.Cells(kRM + 1, kC).Interior.Color = 3394611 '--- vert
wShM.Cells(kRM + 1, kC).Value = wShM.Cells(kRM, kC).Value
Else
wShM.Cells(kRM + 1, kC).Interior.Color = 65535 '--- jaune
End If
End If
Next kC
End If
Loop
fin:
Set Rng2 = Nothing
Set Rng1 = Nothing
Set wShM = Nothing
Set wSh2 = Nothing
Set wSh1 = Nothing
Set wSh = Nothing
End Sub |
Partager