Excel 当满足两个条件时,查找重复条目并对其进行标记

Excel 当满足两个条件时,查找重复条目并对其进行标记,excel,vba,Excel,Vba,当两列中的条件满足时,VBA中是否有方法查找重复条目 我有两列数据。第一列有日期,第二列有金额。问题是在相应列中查找并突出显示所有金额重复且日期相同的金额 到目前为止,我已经找到了一个代码来突出显示1个条件上的重复项 这是密码 Sub RemoveDuplicateAmounts() Dim cel As Variant Dim myrng As Range Set myrng = Sheets("Sheet1").Range("D2:D" & Sh

当两列中的条件满足时,VBA中是否有方法查找重复条目

我有两列数据。第一列有日期,第二列有金额。问题是在相应列中查找并突出显示所有金额重复且日期相同的金额

到目前为止,我已经找到了一个代码来突出显示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:

=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假设为基于给定行前后的D日期和E金额的日期。根据需要更改C5和C4以适合您的数据集

要得到正确/错误的陈述,我要说的是,在前面加上一个0:

对于使用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假设为基于给定行前后的D日期和E金额的日期。根据需要更改C5和C4以适合您的数据集

要得到正确/错误的陈述,我要说的是,在前面加上一个0:


这是一个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

这是一个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


为什么不通过菜单Data | RemovedDuplicates将其删除?嗨,我需要处理这些副本,以便无法删除它们。如果是这样的话,我会选择两列,然后按remove duplicates,这样就可以了。你能添加额外的列吗?如果是这样的话,我可以连接日期和金额,并使用带有条件格式的COUNTIF函数。我会使用sumifs。检查日期和金额,然后对日期求和。除非您正在进行历史核算,否则当不存在匹配项时,总和应为0,当存在匹配项时,总和应为更多。使用基于实现的公式的条件格式规则。为什么不通过菜单Data | RemoveDuplicates将其删除?您好,我需要处理这些重复项,以便无法删除它们。如果是这样的话,我会选择两列,然后按remove duplicates,这样就可以了。你能添加额外的列吗?如果是这样的话,我可以连接日期和金额,并使用带有条件格式的COUNTIF函数。我会使用sumifs。检查日期和金额,然后对日期求和。除非您正在进行历史核算,否则如果没有匹配项,则总和应为0,如果有匹配项,则总和应为更多。使用基于实现的公式的条件格式规则。此外,为了真正谨慎起见,我将使用RangeD65536.EndxlUp.Row代替常数1048576。公式方式对我没有好处。我试着在1000行中通过For循环输入这个公式,结果文件崩溃了。没有足够的内存来处理它。此外,该文件有大约15000行要检查,因此这不适用。函数需要在那里,但只需一秒钟。你可以先插入然后固定值。你说的插入和固定值是什么意思?Cell.FormulaR1C1=[something];然后Cell.Value2=Cell.Value2Plus为了真正谨慎起见,我会使用RangeD65536.EndxlUp.Row来代替常数1048576。公式方法对我没有好处。我试着在1000行中通过For循环输入这个公式,结果文件崩溃了。没有足够的内存来处理它。此外,该文件有大约15000行要检查,因此这不适用。函数需要在那里,但只需一秒钟。你可以先插入然后固定值。你说的插入和固定值是什么意思?Cell.FormulaR1C1=[something];然后Cell.Value2=Cell.Value2这就是我一直在寻找的杰作。宏在几秒钟内处理所有数据。所有结果都是我需要的,即,两个标准匹配,相同的金额和相同的生效日期对应于每个金额。非常感谢你!CheersHi@user3819867真的很有效,我想知道如何添加更多的标准3,4,5。。同样的。你能帮我一下吗?CountIf的参数成对出现,加上[RangeCells1,col{n},CellsLastRow,col{n},Cellsi,col{n}],其中col{n}是第n列,作为寻找匹配项的长整数。对于所有实际用途来说,这应该足够了,我认为它上升到n=26。这就是我一直在寻找的杰作。宏在几秒钟内处理所有数据。所有结果都是我需要的,即,两个标准匹配,相同的金额和相同的生效日期对应于每个金额。非常感谢你!CheersHi@user3819867真的很有效,我想知道如何添加更多的标准3,4,5。。同样的。你能帮我一下吗?CountIf的参数成对出现,加上[RangeCells1,col{n},CellsLastRow,col{n},Cellsi,col{n}],其中col{n}是第n列,作为寻找匹配项的长整数。对于所有实际用途来说,这应该足够了,我认为它上升到n=26。
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