检查列中的单元格是否重复,并检查另一列中的单元格是否为0 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 

这个为我工作。 捡起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 

testing你的:

 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循环重build:

 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 

这个和你提供的那个都是为我工作的。 我有手动input到col(X)和col(AB)的值来testing…确保您已正确格式化col(AB),以便它拾取零和一个数字,而不是一个string。