2015-07-10 78 views
0

我需要獲取列中的所有重複項。該列當前已排序。我相信這需要是一個宏(也許是一個函數),以便它可以將重複值添加到另一列。我想在列A中粘貼一系列單元格,並讓列B給出重複項。下面的例子。獲取列中所有重複項的列表

列A

SD1000023

SD1000024

SD1000024

SD1000025

SD1000026

SD1000026

SD1000027

然後,我列將有:

列B

SD1000024

SD1000026

here我發現THI s,只是不知道如何解決它以滿足我的需求。我不明白list1部分。

+0

=COUNTIF(A:A,A2)和過濾器的List1是包含要在其上操作的值範圍的*姓名*。在那個例子中,List1引用單元格A2:A20。對於新列表,您只需重新定義範圍即可。 –

+0

@EmacsUser,我對'CountIf'有些熟悉,第一個參數是範圍,第二個參數是要查找的表達式 - 爲什麼它是在上面這個例子中再次用作表達式的範圍? – OneFineDay

+0

您可以指向該頁面中「如何使用此數組公式」工作的位置,因爲List1已使用4次,而且我不確定您指的是哪個表達式。 –

回答

2

您可以在「數據」選項卡中使用「刪除重複」應用程序。首先,複製A列,粘貼到B列中。然後選擇Data - > Remove Duplicates並選擇B列(當被問到時,不要擴展到A列)。然後點擊「確定」,這將爲您提供唯一值列表。然後,在那些中,要查看哪些有重複,可以(在C列中)執行「= Countif(B1,A:A)」以查看該值出現在列表中的次數。

這是一個應該讓你去的宏。請注意,我假設你有一個標題行,如果不是你只需要稍微調整:

Sub List_Duplicates() 
Dim lastRow As Integer, dataCol As Integer, duplicateCol As Integer, lastUniqueRow As Integer 
Dim dataRng As Range, dupRng As Range 
Dim ws   As Worksheet 
Set ws = ActiveSheet 
With ws 
    lastRow = .Cells(1, 1).End(xlDown).Row 'This assumes your Column A has no line breaks, if it does, comment this out and use below 
    'lastRow = .UsedRange.Rows.Count 
    dataCol = 1    'if your list of data, including duplicates, is in COlumn A 
    duplicateCol = 2   'where to put the duplicate number 

    Set dataRng = .Range(.Cells(2, dataCol), .Cells(lastRow, dataCol)) 
    Set dupRng = .Range(.Cells(2, duplicateCol), .Cells(lastRow, duplicateCol)) 

    'First, we will copy the Column A data to column B 
    dupRng.Value = dataRng.Value 

    'Now, remove duplicates from this 

    lastUniqueRow = .Cells(2, duplicateCol).End(xlDown).Row 
    dupRng.RemoveDuplicates Columns:=1, Header:=xlNo 
    'Evaluate the countif formula, and then leave duplicates in col. B 
    Dim i   As Integer 
    For i = 2 To lastUniqueRow 'This will loop through our duplicate cells, and remove any that are not duplicates 
     If .Cells(i, duplicateCol).Row > lastUniqueRow Then Exit For 
     Debug.Print .Cells(i, duplicateCol).Value & " occurrs " & Evaluate(WorksheetFunction.CountIf(dataRng, .Cells(i, duplicateCol))) & " times." 
     If Evaluate(WorksheetFunction.CountIf(dataRng, .Cells(i, duplicateCol))) <= 1 Then 
      .Cells(i, duplicateCol).Value = "" 
     End If 
    Next i 
    dupRng.Select 
    dupRng.SpecialCells(xlCellTypeBlanks).Delete 
End With 
End Sub 

讓我知道如果您有任何問題!如果任何人有上述任何意見或建議,請讓我知道這樣我就可以學得:P

+0

謝謝,我希望有一些事情比我每次需要做這些時都要做的更加自動化。 – OneFineDay

+0

我需要重複列表而不是唯一的列表 - 或者我錯過了您的示例中的某些內容? – OneFineDay

+0

如果您執行「Countif」公式,它會告訴您每條記錄在原始列表中顯示的次數。所以,顯示2個或更多的任何內容都是重複的。然後你可以刪除那些不重複的。 如果這一切都適合你,我可以爲VBA編寫一些東西,這樣你就可以將它作爲一個宏來使用。 – BruceWayne

0

這可能幫助:

假設數據在A1開始,在B1輸入

=IF(A1 = A2, A1,"") 

並且在B2中輸入

=IF(AND(A2<>A1,A2 = A3),A2,"") 

並複製此公式的方式休息下來B.這將導致重複值出現在B列一次。然後,您可以將它們複製並粘貼回原位,然後對結果進行排序,以獲得想要聚集到相鄰單元格中的內容。

0

條件格式列A用於重複。這個宏將會在B列中標出所有重複項。

Sub dup() 

Dim lastRow As Long 
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row 

For i = 1 To lastRow 
If Cells(i, 1).DisplayFormat.Interior.Color <> 16777215 Then 
Cells(i, 2) = "Dup" 
End If 
Next 

End Sub 
0

在列旁邊,則可以使用上的任何內容不等於1