当满足2个条件并标记它们时,查找重复条目

在2列中的条件满足时,VBA中有find重复条目的方法吗?

我有两列数据。 第一列有date,第二列有数量。 问题是find并突出显示在相应的列中有重复的金额和相同的date的所有金额?

到目前为止,我已经设法find一个代码来突出显示1条标准的重复。

这是代码

Sub RemoveDuplicateAmounts() Dim cel As Variant Dim myrng As Range Set myrng = Sheets("Sheet1").Range("D2:D" & Sheets("Sheet1").Range("D65536").End(xlUp).Row) myrng.Interior.ColorIndex = xlNone For Each cel In myrng clr = 10 If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then cel.Interior.ColorIndex = 26 clr = clr + 10 End If Next MsgBox ("All duplicates found and coloured") End Sub 

这是一个VBA尝试,我已经给出了同样的东西。 我不认为这是必要的,但OP可能会从中学习。 干杯!

 Sub ertdfgcvb() Dim LastRow As Long, DatesCol As Long, AmountsCol As Long, a As Double, b As Double LastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, Searchdirection:=xlPrevious).Row DatesCol = 4 'D column with dates AmountsCol = 5 'E column with amounts Columns(DatesCol).Interior.ColorIndex = xlNone 'dates lose color For i = 1 To LastRow 'for each row If i <> 1 Then 'had some fun with row 0 error a = Application.WorksheetFunction.SumIfs( _ Range(Cells(1, DatesCol), Cells(i - 1, DatesCol)), _ Range(Cells(1, DatesCol), Cells(i - 1, DatesCol)), _ Cells(i, DatesCol), _ Range(Cells(1, AmountsCol), Cells(i - 1, AmountsCol)), _ Cells(i, AmountsCol)) 'counts the date values associated with recurrences before Else a = 0 'if it's first row I declared a zero, I don't know why End If If i <> LastRow Then 'yeah, last row stuff b = Application.WorksheetFunction.SumIfs( _ Range(Cells(i + 1, DatesCol), Cells(LastRow, DatesCol)), _ Range(Cells(i + 1, DatesCol), Cells(LastRow, DatesCol)), _ Cells(i, DatesCol), _ Range(Cells(i + 1, AmountsCol), Cells(LastRow, AmountsCol)), _ Cells(i, AmountsCol)) 'counts the date values associated with recurrences after Else b = 0 'if it's the last row, there are definitely none after End If If a <> 0 Or b <> 0 Then Cells(i, 4).Interior.ColorIndex = 26 'if either one of them isn't 0 then the date value gets a nice background color Next i End Sub 

有了Countifs和一些优化,它会看起来像这样:

 Sub ertdfgcvb() Dim LastRow As Long, DatesCol As Long, AmountsCol As Long LastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, Searchdirection:=xlPrevious).Row DatesCol = 4 'D column with dates AmountsCol = 5 'E column with amounts Columns(DatesCol).Interior.ColorIndex = xlNone 'dates lose color For i = 1 To LastRow 'for each row If 1 < Application.WorksheetFunction.CountIfs(Range(Cells(1, DatesCol), Cells(LastRow, DatesCol)), _ Cells(i, DatesCol), _ Range(Cells(1, AmountsCol), Cells(LastRow, AmountsCol)), _ Cells(i, AmountsCol)) _ Then Cells(i, 4).Interior.ColorIndex = 26 ' 'counts the date values associated with occurrences if there's more than one then the date gets a nice background color Next i End Sub 
 =SUMIFS($D$1:$D1,$D$1:$D1,$D2,$E$1:$E1,$E2)+SUMIFS($D3:$D$1048576,$D3:$D$1048576,$D2,$E3:$E$1048576,$E2) 

而对于使用VBA:

 =SUMIFS(R1C4:R[-1]C4,R1C4:R[-1]C4,RC4,R1C5:R[-1]C5,RC5)+SUMIFS(R[1]C4:R1048576C4,R[1]C4:R1048576C4,RC4,R[1]C5:R1048576C5,RC5) 

基于给定行之前和之后的D (date)和E (量)的这些总和D (假设为date)。 根据需要更改C5C4以适合您的数据集。

为了得到真实/虚假的陈述,我只是说一个0<>之前:

 =0<>SUMIFS(R1C4:R[-1]C4,R1C4:R[-1]C4,RC4,R1C5:R[-1]C5,RC5)+SUMIFS(R[1]C4:R1048576C4,R[1]C4:R1048576C4,RC4,R[1]C5:R1048576C5,RC5)