设置条件格式-底部边缘

时间:2018-10-04 11:22:51

标签: excel vba excel-vba

我想为$ B2 <> $ B3时的范围设置条件格式。这是我的代码:

Sub AddBorders()
Dim rng As Range
If ActiveSheet.Name <> Sheet1.Name Then
    Exit Sub
End If

Set rng = Range(Range("A2").End(xlToRight), Range("A2").End(xlDown))
'rng.Activate
rng.FormatConditions.Delete


With rng.FormatConditions.Add(xlExpression, xlNotEqual, "=$B2<>$B3")
    With rng.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
    End With
End With
End Sub

好像边界已添加到范围的最底部,我想拥有的是当B列中的值不同时该范围内的边界。当我单击“条件格式”时,我看到已经设置了格式,并在“格式”->“边框”->设置底部边框中手动添加了格式,这正是我想要的。但是我不知道如何正确编码。

1 个答案:

答案 0 :(得分:1)

带走rng,以便您设置条件格式的边界,并将xlEdgeBottom换为xlBottom。

Sub AddBorders()
    Dim rng As Range

    With Worksheets(Sheet1.Name)
        Set rng = .Range(.Range("A2").End(xlToRight), .Range("A2").End(xlDown))
    End With

    With rng
        .FormatConditions.Delete
        With .FormatConditions.Add(Type:=xlExpression, Formula1:="=$B2<>$B3")
            With .Borders(xlBottom)
                .LineStyle = xlContinuous
                .Weight = xlThin
            End With
        End With
    End With
End Sub