将所有边框添加到选定范围内,是否有更短的代码编写方式?

Adding all borders to a selected range, is there a shorter way to write the code?

我将所有边框添加到某个范围内,在我的情况下 (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

试试这个。它将在 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

您可以使用以下语句

Dim myRange As Range
Set myRange = Range("A6:O6")

With myRange.Borders
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With