2016-11-06 59 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