从 if 语句中排除重复项

问题描述 投票:0回答:1

我想排除 A 列中的重复项。

如果该行不重复,我想突出显示该行。

Sub Test2()

Dim count, i As Long

count = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row
i = 2

Do While i <= count

    If Cells(i, 3).Value Like "Expired" Then
        Range(Cells(i, 1), Cells(i, 5)).Interior.Color = RGB(230, 98, 76)
    
    ElseIf Cells(i, 3).Value Like "New" Then
        Range(Cells(i, 1), Cells(i, 5)).Interior.Color = RGB(118, 234, 62)
    
    End If
    
    i = i + 1
Loop

End Sub
excel vba if-statement conditional-statements duplicates
1个回答
0
投票
  • Cells(i, 3).Value Like "New"
    Cells(i, 3).Value = "New"
    相同。

  • 如果您想使用不区分大小写的比较,请尝试

    If StrComp(Cells(i, 3).Value, "New", vbTextCompare)=0 Then

Option Explicit

Sub Test2()
    Dim count as Long, i As Long
    count = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row
    i = 2
    Do While i <= count
        If Application.CountIf(Columns(1), Cells(i, 1)) = 1 Then
            If Cells(i, 3).Value Like "Expired" Then
                Range(Cells(i, 1), Cells(i, 5)).Interior.Color = RGB(230, 98, 76)
            ElseIf Cells(i, 3).Value Like "New" Then
                Range(Cells(i, 1), Cells(i, 5)).Interior.Color = RGB(118, 234, 62)
            End If
        End If
        i = i + 1
    Loop
End Sub

  • 条件格式也是一个选项。

  • CF 公式

=AND($C2="New",COUNTIF($A:$A,$A2)=1)
=AND($C2="Expired",COUNTIF($A:$A,$A2)=1)

© www.soinside.com 2019 - 2024. All rights reserved.