Sub Comparer()
Dim F1 As Worksheet, F2 As Worksheet, d As Object
Dim col1 As Variant, col2 As Variant, lig1 As Variant, lig2 As Variant, dercol%
Dim tablo(), i&, j%, form$, opt$, v1$, v2$, X As Range, rouge As Range, vert As Range
Application.ScreenUpdating = False
'---initialisation---
Cells.ClearContents
Cells.Interior.ColorIndex = xlNone
On Error Resume Next
Set F1 = Workbooks("ancien.xls").Sheets(1)
If Err Then MsgBox "Ouvrez 'ancien.xls' !": Exit Sub
Set F2 = Workbooks("nouveau.xls").Sheets(1)
If Err Then MsgBox "Ouvrez 'nouveau.xls' !": Exit Sub
On Error GoTo 0
'---options classées sans doublon---
Set d = CreateObject("Scripting.Dictionary")
col1 = F1.Cells(1, F1.Columns.Count).End(xlToLeft).Column
col2 = F2.Cells(1, F2.Columns.Count).End(xlToLeft).Column
For i = 1 To col1
d(F1.Cells(1, i).Value) = F1.Cells(1, i).Value
Next
For i = 1 To col2
d(F2.Cells(1, i).Value) = F2.Cells(1, i).Value
Next
Cells(1, 1).Resize(, d.Count) = d.keys
Cells(1, 2).Resize(, col1 + col2 - 1).Sort [B1], Header:=xlNo, Orientation:=xlLeftToRight
'---formules classées sans doublon---
Set d = CreateObject("Scripting.Dictionary")
lig1 = F1.Cells(F1.Rows.Count, 1).End(xlUp).Row
lig2 = F2.Cells(F2.Rows.Count, 1).End(xlUp).Row
For i = 1 To lig1
d(F1.Cells(i, 1).Value) = F1.Cells(i, 1).Value
Next
For i = 1 To lig2
d(F2.Cells(i, 1).Value) = F2.Cells(i, 1).Value
Next
Cells(1, 1).Resize(d.Count) = Application.Transpose(d.keys)
Cells(2, 1).Resize(lig1 + lig2 - 1).Sort [A2], Header:=xlNo, Orientation:=xlTopToBottom
'---remplissage et couleurs---
dercol = Cells(1, Columns.Count).End(xlToLeft).Column
ReDim tablo(1, dercol - 1)
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
form = Cells(i, 1)
lig1 = IIf(i = 1, 1, Application.Match(form, F1.[A:A], 0))
lig2 = IIf(i = 1, 1, Application.Match(form, F2.[A:A], 0))
For j = 1 To dercol
If i = 1 Then
opt = Cells(1, j)
tablo(0, j - 1) = Application.Match(opt, F1.[1:1], 0)
tablo(1, j - 1) = Application.Match(opt, F2.[1:1], 0)
End If
col1 = IIf(j = 1, 1, tablo(0, j - 1))
col2 = IIf(j = 1, 1, tablo(1, j - 1))
v1 = "": v2 = ""
If IsNumeric(lig1) And IsNumeric(col1) Then v1 = F1.Cells(lig1, col1)
If IsNumeric(lig2) And IsNumeric(col2) Then v2 = F2.Cells(lig2, col2)
If v2 = "X" Then _
Set X = Union(IIf(X Is Nothing, Cells(i, j), X), Cells(i, j))
If v1 <> "" And v2 = "" Then _
Set rouge = Union(IIf(rouge Is Nothing, Cells(i, j), rouge), Cells(i, j))
If v1 = "" And v2 <> "" Then _
Set vert = Union(IIf(vert Is Nothing, Cells(i, j), vert), Cells(i, j))
Next
Next
If Not X Is Nothing Then X = "X"
If Not rouge Is Nothing Then rouge.Interior.ColorIndex = 3
If Not vert Is Nothing Then vert.Interior.ColorIndex = 4
End Sub