将所有边界添加到选定范围,是否有更简单的方法来编写代码?

我将所有边界添加到一定的范围,在我的情况下(A6:O6),在Excel VBA中,下面的代码工作,但我会想象会有一个更短的方式来写它。 我发现了一行代码,在整个选区周围放置了一个边界,但是并没有围绕每个单元。

Range("A6:O6").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With 

你可以使用下面的语句

 Dim myRange As Range Set myRange = Range("A6:O6") With myRange.Borders .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With 

尝试这个。 它将在范围A6:O6的每个单元格周围添加一个边框。

 Sub Macro1() Dim rng As Range ' Define range Set rng = Range("A6:O6") With rng.Borders .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = 0 .TintAndShade = 0 End With End Sub