今回はVBAでセルの罫線を設定する方法を解説します。
罫線の設定は少しわかりにくいところがあるので、誰でもわかるように徹底的に説明していきます!
また、本記事では罫線の名前を図をセットにして解説を行っていますので、罫線の名前(種類)がわからない人でも大丈夫です!
罫線の種類
Excelの書式設定を確認すると罫線は全13種類あることがわかります。


うめ
13種類の罫線を設定するコードの説明していきます!
罫線の一覧

番号を振っただけではイマイチわかりにくいので、設定した罫線とセットでまとめました。

No | 種類 | LineStyle | Weight |
❶ | 極細 | – | xlHairline |
❷ | 点線 | xlDot | – |
❸ | 二点鎖線 | xlDashDotDot | – |
❹ | 一点鎖線 | xlDashDot | – |
❺ | 破線 | xlDash | – |
❻ | 実線 | xlContinuous | – |
❼ | 二点鎖線(中) | xlDashDotDot | xlMedium |
❽ | 斜線 | xlSlantDashDot | – |
❾ | 一点鎖線(中) | xlDashDot | xlMedium |
❿ | 点線(中) | xlDash | xlMedium |
⓫ | 中線 | – | xlMedium |
⓬ | 太線 | – | xlThick |
⓭ | 二重線 | – | xlDouble |
罫線のコード
本記事では設定した罫線を見やすくするため、Excelの「目盛線」を消した状態での設定しています。
極細
1 2 3 4 5 6 7 8 |
Sub sample1() '極細 With Range("C3").Borders .Weight = xlHairline End With End Sub |

点線
1 2 3 4 5 6 7 8 |
Sub sample2() '点線 With Range("C3").Borders .LineStyle = xlDot End With End Sub |

二点鎖線
1 2 3 4 5 6 7 8 |
Sub sample3() '二点鎖線 With Range("C3").Borders .LineStyle = xlDashDotDot End With End Sub |

一点鎖線
1 2 3 4 5 6 7 8 |
Sub sample4() '一点鎖線 With Range("C3").Borders .LineStyle = xlDashDot End With End Sub |

破線
1 2 3 4 5 6 7 8 |
Sub sample5() '破線 With Range("C3").Borders .LineStyle = xlDash End With End Sub |

実線
1 2 3 4 5 6 7 8 |
Sub sample6() '実線 With Range("C3").Borders .LineStyle = xlContinuous End With End Sub |

二点鎖線(中)
1 2 3 4 5 6 7 8 9 |
Sub sample7() '二点鎖線(中) With Range("C3").Borders .LineStyle = xlDashDotDot .Weight = xlMedium End With End Sub |

斜線
1 2 3 4 5 6 7 8 |
Sub sample8() '斜線 With Range("C3").Borders .LineStyle = xlSlantDashDot End With End Sub |

一点鎖線(中)
1 2 3 4 5 6 7 8 9 |
Sub sample9() '一点鎖線(中) With Range("C3").Borders .LineStyle = xlDashDot .Weight = xlMedium End With End Sub |

点線(中)
1 2 3 4 5 6 7 8 9 |
Sub sample10() '点線(中) With Range("C3").Borders .LineStyle = xlDash .Weight = xlMedium End With End Sub |

中線
1 2 3 4 5 6 7 8 |
Sub sample11() '中線 With Range("C3").Borders .Weight = xlMedium End With End Sub |

太線
1 2 3 4 5 6 7 8 |
Sub sample12() '太線 With Range("C3").Borders .Weight = xlThick End With End Sub |

二重線
1 2 3 4 5 6 7 8 |
Sub sample13() '二重線 With Range("C3").Borders .LineStyle = xlDouble End With End Sub |

まとめ
今回はExcelの書式設定に表示されている13種類の罫線を設定するためのコードを紹介しました。

うめ
全てのコードを覚える必要はありません。
罫線は「LineStyle」と「Weight」の組み合わせで制御する。と覚えておきましょう!