首页 > 解决方案 > 突出显示基于 pf 列标准的行 VBA

问题描述

我正在尝试编写一个 VBA 脚本来比较两个 = 行,并让电子表格仅在满足某些条件时才突出显示重复的行,例如(行的值,列 a = 第 1 行的值,列)和行的值, b 列 > 第 1 行的值,b 列)然后 b.font.color 列中较大值的整行 = vbRed。

这是我正在运行的表格的一部分...

表选择

这是我正在使用的代码...

Sub RemoveDuplicates()

Dim i As Long, R As Long
'Dim DeviceName As Range, SerialNumber As Range, LastContact As Range

Application.ScreenUpdating = False

R = Cells(Rows.Count, 1).End(xlUp).Row

'Set DeviceName = Columns(2)
'Set SerialNumber = Columns(3)
'Set LastContact = Columns(7)

For i = R To 2 Step -1

'If Cells(i, "F").Value > Cells(i - 1, "F").Value Then
'Code above doesn't work

If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value Then

    'If Cells(i, 3).Value = Cells(i - 1, 3).Value And Cells(i, 2).Value = Cells(i - 1, 2).Value And Cells(i, 5).Value > Cells(i - 1, 5).Value Then
    'Code above doesn't work

    Cells(i, 1).EntireRow.Font.Color = vbRed
    End If
Next i

Application.ScreenUpdating = True

End Sub

我可以让重复项突出显示,但是当我尝试引入大于检查时,系统会出现问题。

在此处输入图像描述

标签: excelvbaduplicates

解决方案


尝试条件格式规则。

With worksheets("sheet1").usedrange.offset(1, 0).entirerow
    .FormatConditions.Delete
    With .FormatConditions.Add(Type:=xlExpression, Formula1:="=and($a2=$a1, $b2=$b1, $f2>$f1)")
        .font.Color = vbRed
    End With
End With

推荐阅读