多个等于单元格,然后chek偏移单元格

时间:2016-06-24 18:13:24

标签: excel vba excel-vba

我想检查B列(PIB)中的所有ID,如果有重复的ID来检查A列(REGION),那么这些PIB是否有多个区域。我想检查PIB的重复区域是否具有唯一性或让我们说不同的区域。 enter image description here

我坚持使用以下代码标记B列并重复:

Option Explicit
Sub Test()

Dim rng As Range, rng2 As Range, rng3 As Range, cell As Range, i As Integer

Set rng = Range("A2:D400")
Set rng2 = Range("B2:B400")
Set rng3 = Range("A2:A400")

For Each cell In rng
    If Application.WorksheetFunction.CountIf(rng2, cell) > 1 Then
        cell.Interior.ColorIndex = 26
    End If
Next
End Sub

从这段代码中,我尝试添加一些嵌套的IF,但后来没有任何反应:

Option Explicit
Sub Test()

Dim rng As Range, rng2 As Range, rng3 As Range, cell As Range, i As Integer

Set rng = Range("A2:D400")
Set rng2 = Range("B2:B400")
Set rng3 = Range("A2:A400")

For Each cell In rng
    If Application.WorksheetFunction.CountIf(rng2, cell) > 1 Then
        If Application.WorksheetFunction.CountIf(rng3, cell) > 1 Then
        cell.Interior.ColorIndex = 26
    End If
    End If
Next
End Sub

谢谢!

1 个答案:

答案 0 :(得分:1)

非VBA解决方案(因为看起来你没问题)。

  1. C1 = Check,E1 ='检查',E2 = >0
  2. C2 = =IF(COUNTIFS(B:B,B2,A:A,"<>"&A2)>0,1,0),然后向下拖动
  3. enter image description here

    1. 高级过滤器,如下所示:
    2. enter image description here