检查列中的单元格是否重复,并检查另一列中的单元格是否为0 vba

时间:2017-10-20 15:39:29

标签: excel vba excel-vba

我需要检查列(X:X)中的单元格是否重复,以及另一列(AB:AB)中的单元格是否为0,如果条件匹配,则突出显示颜色中的相应行。这就是我所拥有的,但它不起作用..

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

2 个答案:

答案 0 :(得分:2)

这个对我有用。选择0"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

答案 1 :(得分:1)

经过测试:

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

使用for循环重建:

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

这个和你提供的那个都为我工作;我已经手动将值输入col(X)和col(AB)来测试...确保你已正确格式化了col(AB),以便它获取零和一个数字,而不是一个字符串。