2015-05-05 66 views
2

我是新的Excel VBA,我真的需要你的幫助。我有一個代碼將在列A中查找重複值。此代碼將突出顯示重複值。我想要:Excel VBA-重複運行按鈕/添加位置

1)這個代碼只有當我點擊一個按鈕時纔會運行。

2.)我想有(在同一個工作表中的某個地方),重複結果的數量和一個超鏈接,當你點擊它時將指導你重複的結果(這是因爲我有時有巨大的文件我需要驗證)。這裏是我目前的代碼:

Sub Worksheet_Change(ByVal Target As Excel.Range) 
Dim C As Range, i As Long 
If Not Intersect(Target, Me.[A:A]) Is Nothing Then 
Application.EnableEvents = False 
For Each C In Target 
    If C.Column = 1 And C.Value > "" Then 
     If WorksheetFunction.CountIf(Me.[A:A], C.Value) > 1 Then 
     i = C.Interior.ColorIndex 
     f = C.Font.ColorIndex 
     C.Interior.ColorIndex = 3 ' Red 
     C.Font.ColorIndex = 6 ' Yellow 
      C.Select 
      MsgBox "Duplicate Entry !", vbCritical, "Error" 
     C.Interior.ColorIndex = i 
     C.Font.ColorIndex = f 
     End If 
    End If 
Next 
Application.EnableEvents = True 
End If 
End Sub 

我真的很感激,如果你幫我這個。

+1

嘗試添加一個按鈕,你的工作......你先碼出'Worksheet_Change'事件,並創建一個帶有Public Sub MyButton()的模塊。接下來,從Developer選項卡向工作表中添加一個按鈕(任何地方都會這樣做),並且系統會提示您將按鈕連接到宏。一旦你連接它,你應該被設置。 – PeterT

+0

感謝您的回覆。這是行不通的。也許我沒有正確修改代碼。我在同一張表中有其他按鈕和其他代碼,這是爲什麼? – LillieG

+0

如果你在@PeterT的評論中關注這個建議,你還需要明確地將Target設置爲'A:A'並且取出'Intersect'測試。 – Comintern

回答

1

的代碼添加到Module Alt鍵 + F11

Option Explicit 

Sub MyButton() 
    Dim RangeCell As Range, _ 
    MyData As Range 
    Dim MyDupList As String 
    Dim intMyCounter As Integer 
    Dim MyUniqueList As Object 
    Dim lngLastRow As Long, lngLoopRow As Long 
    Dim lngWriteRow As Long 

    Set MyData = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row) 
    Set MyUniqueList = CreateObject("Scripting.Dictionary") 

    Application.ScreenUpdating = False 
    MyDupList = "": intMyCounter = 0 
    '// Find Duplicate 
    For Each RangeCell In MyData 
     If RangeCell <> "V" And RangeCell <> "R" Then 
      If Evaluate("COUNTIF(" & MyData.Address & "," & RangeCell.Address & ")") > 1 Then 
       '// Color. Change to suit RGB(141, 180, 226). 
       RangeCell.Interior.Color = RGB(141, 255, 226) 
       If MyUniqueList.exists(CStr(RangeCell)) = False Then 
        intMyCounter = intMyCounter + 1 
        MyUniqueList.Add CStr(RangeCell), intMyCounter 
        If MyDupList = "" Then 
         MyDupList = RangeCell 
        Else 
         MyDupList = MyDupList & vbNewLine & RangeCell 
        End If 
       End If 
      Else 
       RangeCell.Interior.ColorIndex = xlNone 
      End If 
     End If 
    Next RangeCell 
    '// Move duplicate from Column 1 to Column 7 = (G:G) 
    lngWriteRow = 1 
    lngLastRow = Cells(Rows.Count, 1).End(xlUp).Row 
    For lngLoopRow = lngLastRow To 1 Step -1 
     With Cells(lngLoopRow, 1) 
      If WorksheetFunction.CountIf(Range("A1:A" & lngLastRow), .Value) > 1 Then 
       If Range("G:G").Find(.Value, lookat:=xlWhole) Is Nothing Then 
        Cells(lngWriteRow, 7) = .Value 
        lngWriteRow = lngWriteRow + 1 
       End If 
      End If 
     End With 
    Next lngLoopRow 

    Set MyData = Nothing: Set MyUniqueList = Nothing 

    Application.ScreenUpdating = False 

    If MyDupList <> "" Then 
     MsgBox "Duplicate entries have been found:" & vbNewLine & MyDupList 
    Else 
     MsgBox "There were no duplicates found in " & MyData.Address 
    End If 
End Sub 

Add Module 

enter image description here

Add Button 

enter image description here

Assign to Macro 

enter image description here

+0

謝謝奧馬,你的代碼工作。我只想突出顯示A列中的重複項,而不是將兩個列與重複項進行比較。你知道我該如何解決我的第二個問題嗎?:2)我想有(在同一個工作表中的某個地方),重複結果的數量和一個超鏈接,當你點擊它時會指導你重複的結果這是因爲我有時需要驗證大文件)。 – LillieG

+1

謝謝Omar !,這個作品很棒:) – LillieG