2016-11-06 62 views
1

我正在添加所有边界到一定的范围,在我的情况下(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 
+0

你想每一个细胞周围的边框? – Niclas

回答

2

您可以使用下面的语句

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

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

试试这个。它将在范围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 
+0

Upvoted先入先出。 – brettdj