简体   繁体   English

Excel VBA通​​过条件识别重复项

[英]Excel VBA Identifying duplicates with a condtion

Quite new to this, so still trying to get my footing. 这很新,所以仍要努力争取立足之地。

I'm Looking for a rule, where any duplicates within a column is identified, and then, if it is, whether in another column, those two duplicates sum to zero. 我正在寻找一个规则,其中标识一列中的所有重复项,然后(如果是)是否在另一列中,这两个重复项的总和为零。

For example: 例如:

例

In the basic image example, the first two rows will be identified as a match whereas the last two will not, because they don't sum to zero. 在基本图像示例中,前两行将被标识为匹配项,而后两行将不被标识为匹配项,因为它们的总和不为零。

I've used the following code that will highlight a single column, however, I'm not sure how to encorporate an additional rule where the sum of two duplicates is 0: 我使用了下面的代码来突出显示一列,但是,我不确定如何合并两个重复的总和为0的附加规则:

Sub Duplicate_Shade()
`Dim cel As Variant
 Dim myrng As Range
 Set myrng = Range("A2:A" & Range("A10000").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
 End Sub

Any help would be greatly appreciated 任何帮助将不胜感激

A fairly minimal change to your code would be: 对您的代码进行的最小更改是:

Sub Duplicate_Shade()
    Dim myrng As Range
    Set myrng = Range("A2:A" & Range("A10000").End(xlUp).Row)
    myrng.Interior.ColorIndex = xlNone
    For Each cel In myrng
        If Application.WorksheetFunction.CountIf(myrng, cel) > 1 And _
           Application.WorksheetFunction.SumIf(myrng, cel, myrng.Offset(, 1)) = 0 Then
            cel.Interior.ColorIndex = 26
        End If
    Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM