2017-10-20 16 views
1

Je dois vérifier si une cellule dans une colonne (X: X) est un double et si une cellule dans une autre colonne (AB: AB) est 0, et si les conditions correspondent, mettez en surbrillance la ligne correspondante dans une couleur. Voilà ce que j'ai, mais cela ne fonctionne pas ..Vérifiez si la cellule dans la colonne est double et vérifier si la cellule dans une autre colonne est 0 vba

Dim cell1 As Variant, myrngg1 As Range, clr1 As Long 
Set myrngg1 = Range("X1:X" & Cells(Rows.count, "X").End(xlUp).Row) 
clr1 = 1 
For Each cell1 In myrngg1 
If Application.WorksheetFunction.CountIf(myrngg1, cell1) > 1 And Range("AB" & clr1).Value = 0 Then 
    cell1.EntireRow.Interior.Color = vbGrey 
End If 
clr1 = clr1 + 1 
Next 
+0

Pourquoi ne pas utiliser Mise en forme conditionnelle? –

+0

@ScottCraner Il est une partie d'une plus grande macro qui devrait automatiser une opération de recherche manuelle –

+0

l'option Activer explicite et vous verriez que vous avez une faute de frappe dans votre nom de variable 'cell'. – SJR

Répondre

2

celui-ci fonctionne pour moi. reprend à la fois 0 et "0"

Option Explicit 

Sub test() 

    Dim cell1 As Variant, myrngg1 As Range 
    Set myrngg1 = Range("X1:X" & Cells(Rows.Count, "X").End(xlUp).Row) 

    For Each cell1 In myrngg1 
     If Application.WorksheetFunction.CountIf(myrngg1, cell1) > 1 And cell1.EntireRow.Columns("AB").Value & "" = "0" Then 
      cell1.EntireRow.Interior.Color = rgbGrey ' vbGrey is undefined in my version of excel 
     End If 
    Next 

End Sub 
+0

Cela fonctionne: D Merci! –

1

vôtre: Testée

Dim cell1 As Variant, myrngg1 As Range, clr1 As Long 
Set myrngg1 = Range("X1:X" & Cells(Rows.count, "X").End(xlUp).Row) 
clr1 = 1 
For Each cell1 In myrngg1 
If Application.WorksheetFunction.CountIf(myrngg1, cell1) > 1 And Range("AB" & clr1).Value = 0 Then 
    cell1.EntireRow.Interior.Color = vbGrey 
End If 
clr1 = clr1 + 1 
Next 

recréées à l'aide d'une boucle:

Dim i as Long, LR as Long 
LR = Cells(Rows.Count, "X").End(xlUp).Row 
For i = LR to 1 Step -1 
    If Application.CountIf(Range(Cells(1,"X"),Cells(LR,"X")), Cells(i,"X").Value) > 1 AND CellS(i, "AB").Value = 0 Then 
     Rows(i).EntireRow.Interior.Colo = vbGrey 
    End If 
Next i 

fois ceci et celui que vous avez fourni travaille pour moi; J'ai manuellement entré les valeurs dans col (X) et col (AB) pour tester ... assurez-vous que vous avez correctement formaté le col (AB) afin qu'il ramasse zéro et un nombre, pas une chaîne.

+0

Si vous rencontrez des problèmes avec le format, utilisez = "0" plutôt que = 0 pour voir s'il corrige quelque chose. – Cyril