LoginSignup
2
1

More than 5 years have passed since last update.

【Excel VBA】視認性の良いシンプルな罫線・フォント設定をするマクロ

Last updated at Posted at 2019-04-01

マクロ実行前後比較

【びふぉー】
image.png
【あふたー】
image.png

何のため

上図のように構造化した情報をリストアップした際、セル結合による視認性向上をしたくないため
(フィルター抽出できなくなるじゃないかああああ)

上のセルと比較してセル内容が等しかったらフォントカラーを薄くし、セル内容が異なっていたら罫線を実線で引きます。
それらの範囲指定がベタ書きなので美しいコードではありません。その都度コード修正。
簡単なコードだからこの程度で良いかーという……。

コード

    Sub 視認性良く罫線とフォント設定()

      '範囲選択
      Range("A1").Select
      Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

      '書式設定を初期化
      Cells.FormatConditions.Delete

      '罫線書式設定
      Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$C1<>$C2"
      Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
      With Selection.FormatConditions(1).Borders(xlBottom)
          .LineStyle = xlContinuous
          .TintAndShade = 0
          .Weight = xlThin
      End With
      Selection.FormatConditions(1).StopIfTrue = False

      'フォント書式設定
      Range("B2").Select
      Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
      Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B2=B1"
      Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
      With Selection.FormatConditions(1).Font
          .ThemeColor = xlThemeColorDark1
          .TintAndShade = -0.249946592608417
      End With
      Selection.FormatConditions(1).StopIfTrue = False

      '最後に罫線設定
      Range("A1").Select
      Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
      '基本的に実線
      With Selection.Borders
          .LineStyle = xlContinuous
          .ColorIndex = xlAutomatic
          .TintAndShade = 0
          .Weight = xlThin
      End With
      '内側水平線のみ点線
      With Selection.Borders(xlInsideHorizontal)
          .Weight = xlHairline
      End With

    End Sub
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1