Comparer 2 colonnes

teodormircea

XLDnaute Occasionnel
Bonjour le forum

J'ai fait une macros qui doit comparer les valeurs entre 2 colonnes , si egalite trouve entre 2 cellules mettre ok dans une autre si non KO.

J'essaye de remplacer la fonction =IF(VAL1=VAL2,"OK","KO")

Code:
Sub format()
 With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
 
       Dim x As Integer
       Dim Y As Integer
       Dim Z As Integer
       Dim r As Range
       Dim s As Range
       Dim t As Range
       
       x = CLng(InputBox(Prompt:="Quelle colonne?"))
       If (x < 1) + (x > Columns.Count) Then Exit Sub
       LastRow = ActiveSheet.Cells(Rows.Count, x).End(xlUp).Row
       Y = CLng(InputBox(Prompt:="Quelle colonne?"))
       If (Y < 1) + (Y > Columns.Count) Then Exit Sub
       LastRow = ActiveSheet.Cells(Rows.Count, Y).End(xlUp).Row
      Z = CLng(InputBox(Prompt:="Quelle colonne?"))
       If (Y < 1) + (Y > Columns.Count) Then Exit Sub
       LastRow = ActiveSheet.Cells(Rows.Count, Y).End(xlUp).Row
       
      
       For Each r In Range(Cells(1, x), Cells(Rows.Count, x).End(xlUp))
       Next
       For Each s In Range(Cells(1, Y), Cells(Rows.Count, Y).End(xlUp))
       Next
       For Each t In Range(Cells(1, Z), Cells(Rows.Count, Z).End(xlUp))
       Next
         If r.Value = s.Value Then
         t.Value = "OK"
         Else
         t.Value = "KO"
         
    

     .Calculation = xlCalculationAutomatic
     .ScreenUpdating = True
    
     End If
Merci d'avance
 

Pierrot93

XLDnaute Barbatruc
Re : Comparer 2 colonnes

Re

renvoie dans la colonne 3 en comparant colonne 1 et 2, sur la base du nombre de lignes de la colonne 1 :

Code:
Option Explicit
Sub test()
Dim i As Integer
For i = 1 To Range("A65536").End(xlUp).Row
    Cells(i, 3).Value = IIf(Cells(i, 1).Value = Cells(i, 2).Value, "OK", "NOK")
Next i
End Sub

@+
 

teodormircea

XLDnaute Occasionnel
Re : Comparer 2 colonnes

Voila le code complet, c'est pour une userform,comp1,comp2 et r c'est des textbox.Le code marche bien mais , j'ai eu la surprise au moment ou j'ai travaille avec des colonnes filtres de voir des résultats sur les zones caches aussi.


Code:
Sub CompareColumns2()
 With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
 
       Dim x As Integer
       Dim Y As Integer
       Dim Z As Integer
       Dim i As Integer
       
       
       x = comp1
       If (x < 1) + (x > Columns.Count) Then Exit Sub
       LastRow = ActiveSheet.Cells(Rows.Count, x).End(xlUp).Row
       Y = comp2
       If (Y < 1) + (Y > Columns.Count) Then Exit Sub
       LastRow = ActiveSheet.Cells(Rows.Count, Y).End(xlUp).Row
      Z = r
       If (Y < 1) + (Y > Columns.Count) Then Exit Sub
       LastRow = ActiveSheet.Cells(Rows.Count, Y).End(xlUp).Row
       
      
      
For i = 2 To LastRow
    If Cells(i, x).Hidden = True Or Cells(i, Y).Hidden = True Then
        Cells(i, Z).Value = IIf(Cells(i, x).Value = Cells(i, Y).Value, "OK", "NOK")
    
    End If
Next i

    


     .Calculation = xlCalculationAutomatic
     .ScreenUpdating = True
    End With
End Sub
 

teodormircea

XLDnaute Occasionnel
Re : Comparer 2 colonnes

Voila la version sans USF.Ca marche bien.Mais je voudrais la faire marcher aussi sur des colonnes filtre et de comparer seulement ce qui est visible.
 

Pièces jointes

  • compare.xls
    26 KB · Affichages: 130
  • compare.xls
    26 KB · Affichages: 125
  • compare.xls
    26 KB · Affichages: 125

Discussions similaires

Statistiques des forums

Discussions
312 392
Messages
2 087 999
Membres
103 691
dernier inscrit
christophe89