2017-02-10 65 views
0

我试过下面的宏代码,但它不是强调从第二个实例重复PLZ帮我出高亮重复值,从孔片范围(UsedRange)

还有一件事IAM努力做与动态不采取固定范围(特定范围)

Sub FindingDuolicate() 
    Dim Rng As Range 
    Dim rngCell As Variant 
    Dim Flag As Long 
    ActiveSheet.UsedRange.Select 
    Flag = 0 
    For Each Rng In Selection 
     If (WorksheetFunction.CountIf(Selection, Rng.Value) > 1) Then 
      Rng.Interior.Color = vbRed 
      Flag = Flag + 1 
     Else 
      Rng.Interior.Pattern = xlNone 
     End If 
    Next 
    If Flag > 0 Then 
     MsgBox Flag & " Cells (in red) Contain an Duplicate Data. Please Check" 
    Else 
     MsgBox " Data Validation Completed . No Duplicate Found. " 
    End If 
End Sub 
+0

'昏暗的标志作为Long'(不' Dim LR');和'xlNone'不是'x1None'(L不是1)。 –

+0

@ A.S.H对不起,这是输入错误,我纠正了这一点。但我需要帮助plz帮助我,如果你能 –

+0

你的宏为我工作与上述更正,虽然没有测试几乎。你怎么调用它?还要添加Option Explicit,这对于帮助你发现许多错误非常有用。 –

回答

1

你可以把你的子成一个函数:

Function FindingDuplicate(rng As Range, counter As Long) As Boolean 
    Dim cell As Range 

    For Each cell In rng 
     If WorksheetFunction.CountIf(Range(rng(1, 1), cell), cell.Value) > 1 Then 
      cell.Interior.Color = vbRed 
      counter = counter + 1 
     Else 
      cell.Interior.Pattern = xlNone 
     End If 
    Next 
    FindingDuplicate = counter > 0 
End Function 

被你的 “主” 子如下利用:

Option Explicit 

Sub main() 
    Dim counter As Long 

    If FindingDuplicate(ActiveSheet.UsedRange, counter) Then '<--| change 'ActiveSheet.UsedRange' to whatever range you want 
     MsgBox counter & " cells (red background) contain a duplicated data. Please Check" 
    Else 
     MsgBox " Data Validation Completed. No Duplicate Found." 
    End If 
End Sub 
+0

谢谢你soo多为你的帮助 –

+0

不客气。那么您可能想要将答案标记为已接受。谢谢! – user3598756

+0

您发送给我的代码,该宏代码不是在字符串和三位数字编号例如: - 444 –

0

已更新的答案。它现在不使用countif,而是循环遍历每个先前的单元格进行比较。如果你有一个非常大的范围,但可以放慢,但它可以在多个柱子上工作。

Sub DupsCheck() 
Dim Rng As Range 
Dim RngChecked As Range 
Dim previousRng As Range 
Dim rngCell As Variant 
Dim LR As Long 

'ActiveSheet.UsedRange.Select 
Flag = 0 

Selection.Interior.Pattern = x1None 

For Each Rng In Selection 
    If Not RngChecked Is Nothing Then 
    ' Add the 2nd, 3rd, 4th etc cell to our new range, rng2 
    ' this is the most common outcome so place it first in the IF test (faster coding) 

    For Each previousRng In RngChecked 

     If previousRng.Value = Rng.Value And Rng.Interior.Color <> vbRed Then 
     Debug.Print previousRng.Address & " " & Rng.Address 
      Rng.Interior.Color = vbRed 
      Flag = Flag + 1 

     End If 
     'Debug.Print Flag 
    Next 

     Set RngChecked = Union(RngChecked, Rng) 
    Else 
    ' the first valid cell becomes rng2 
     Set RngChecked = Rng 
    End If 

Next 
If Flag > 0 Then 
    MsgBox Flag & " Cells (in red) Contain an Duplicate Data. Please Check" 
Else 
    MsgBox " Data Validation Completed . No Duplicate Found. " 
End If 
End Sub 
+0

是它的工作我knw但我需要它将突出显示从二审,plz帮助我 –

+0

我知道你想这样,我我刚刚更新了我的答案。 – Gordon

+0

如果(WorksheetFunction.CountIf(RngChecked,rng.Value)> 1)然后在上面的行运行时错误'5'无效的过程调用或参数 –