检查列是否存在VBA-excel中的重复logging

我是新来的Excel中的VBAmacros,并只想问是否有任何检查Excel中的重复logging的function。

下面这行代码删除了引用A列的重复内容,但是我不想在没有用户确认的情况下删除它,我想要做的就是要求用户确认是否要删除它,比如popup,然后这行只是执行,但我不知道是否有检查重复的function。

ActiveSheet.Range("$A$1:$D$38").RemoveDuplicates Columns:=1 

在此先感谢您的帮助。

请尝试下面的代码。 我已经设置脚本来使重复单元格为空,但是你可以插入你自己的代码。

 Sub FindDuplicates() Dim i As Long Dim j As Long Dim lDuplicates As Long Dim rngCheck As Range Dim rngCell As Range Dim rngDuplicates() As Range '(!!!!!) Set your range Set rngCheck = ActiveSheet.Range("$A$1:$D$38") 'Number of duplicates found lDuplicates = 0 'Checking each cell in range For Each rngCell In rngCheck.Cells Debug.Print rngCell.Address 'Checking only non empty cells If Not IsEmpty(rngCell.Value) Then 'Resizing and clearing duplicate array ReDim rngDuplicates(0 To 0) 'Setting counter to start i = 0 'Starting search method Set rngDuplicates(i) = rngCheck.Find(What:=rngCell.Value, After:=rngCell, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext) 'Check if we have at least one duplicate If rngDuplicates(i).Address <> rngCell.Address Then 'Counting duplicates lDuplicates = lDuplicates + 1 'If yes, continue filling array Do While rngDuplicates(i).Address <> rngCell.Address i = i + 1 ReDim Preserve rngDuplicates(0 To i) Set rngDuplicates(i) = rngCheck.FindNext(rngDuplicates(i - 1)) Loop 'Ask what to do with each duplicate '(except last value, which is our start cell) For j = 0 To UBound(rngDuplicates, 1) - 1 Select Case MsgBox("Original cell: " & rngCell.Address _ & vbCrLf & "Duplicate cell: " & rngDuplicates(j).Address _ & vbCrLf & "Value: " & rngCell.Value _ & vbCrLf & "" _ & vbCrLf & "Remove duplicate?" _ , vbYesNoCancel Or vbExclamation Or vbDefaultButton1, "Duplicate found") Case vbYes '(!!!!!!!) insert here any actions you want to do with duplicate 'Currently it's set to empty cell rngDuplicates(j).Value = "" Case vbCancel 'If cancel pressed then exit sub Exit Sub End Select Next j End If End If Next rngCell 'Final message Call MsgBox("Total number of duplicates: " & lDuplicates & ".", vbExclamation Or vbDefaultButton1, Application.Name) End Sub 

PS如果你只需要在一列内删除dulpicates,你需要调整rngCheckvariables到那个特定的列。

PPS在我看来,使用条件格式更容易。