罫線の種類の設定

広告

罫線の線の種類はBorderオブジェクトの「LineStyle」プロパティで管理されています。デフォルトでは罫線を表示しない「xlLineStyleNone」が設定されていますので罫線を表示したい場合には「LineStyle」プロパティに適切な値を設定します。

Dim border1 As Border

Set border1 = Range("A1").Borders(xlEdgeTop)
border1.LineStyle = xlContinuous

設定可能な罫線の種類は次のようになります。

定数罫線の種類
xlContinuous実線(細)
xlDash破線
xlDashDot一点鎖線
xlDashDotDot二点鎖線
xlDot点線
xlDouble二重線
xlSlantDashDot斜め斜線
xlLineStyleNone無し

罫線は形状の他にも線の太さを設定可能です。Borderオブジェクトの「Weight」プロパティで設定します。

Dim border1 As Border

Set border1 = Range("A1").Borders(xlEdgeTop)
border1.LineStyle = xlContinuous
border1.Weight = xlMedium

設定可能な罫線の太さは次のようになります。

定数罫線の太さ
xlHairline極細
xlThin
xlMedium
xlThick

罫線の線種と太さを組み合わせることで多くの種類の罫線を描く事ができますが、設定が出来ない組み合わせもありますので注意して下さい。

サンプルプログラム

では簡単なサンプルで試してみましょう。

testcellborder2.bas

Sub テスト()
    
    With Range("B2:C3")
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).LineStyle = xlDash
        .Borders(xlEdgeLeft).LineStyle = xlDashDot
        .Borders(xlEdgeRight).LineStyle = xlDouble
    End With
    
    With Range("C3:E7")
        .Borders(xlEdgeTop).LineStyle = xlContinuous
        .Borders(xlEdgeTop).Weight = xlHairline
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeBottom).Weight = xlThin
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeLeft).Weight = xlMedium
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeRight).Weight = xlThick
    End With
    
End Sub

上記マクロを実行すると次のようになります。

罫線の設定

( Written by Tatsuo Ikura )