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

Tri*_*ias 1 excel vba border excel-vba

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

Kar*_*pak 10

您可以使用以下语句

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

With myRange.Borders
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
Run Code Online (Sandbox Code Playgroud)


Nic*_*las 6

试试这个.它将在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
Run Code Online (Sandbox Code Playgroud)