2017-02-10 48 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'無效的過程調用或參數 –