设置条件格式 - 下边缘



我想在 $B 2<>$B 3 时为范围设置条件格式。这是我的代码:

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 列中的值不同时,我想要的是范围内的边框.当我单击"条件格式"时,我看到格式已设置,并在"格式"->"边框"->中手动添加它,设置下边框正是我想要的。但是我不知道如何正确编码。

去掉 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

最新更新