2017-10-20 93 views
1

我需要檢查列(X:X)中的單元格是否重複,並且如果另一列中的單元格(AB:AB)爲0,並且如果條件匹配,則以顏色突出顯示相應的行。這是我有什麼,但它不工作..檢查列中的單元格是否重複,並檢查另一列中的單元格是否爲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

爲什麼不使用條件格式? –

+0

@ScottCraner它是應該自動執行手動搜索操作的更大宏的一部分 –

+0

打開Option Explicit,你會發現你的變量名'cell'中有一個拼寫錯誤。 – SJR

回答

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 
+0

它的工作原理:D謝謝! –

1

測試你的:使用for循環

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 

改建:

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),以便它拾取零和一個數字,而不是一個字符串。

+0

如果您遇到格式問題,請使用=「0」而不是= 0來查看它是否修復了任何內容。 – Cyril

相關問題