テーブル機能の備忘録 ⑧ イケてるスタイル

先日から、マクロによるテーブル機能の扱いを纏めている(備忘録)。
infoment.hatenablog.com

今日は、テーブルのスタイルについてのお話。
f:id:Infoment:20181008093249j:plain

Excelは既に、テーブルのスタイルを幾つか準備してくれている。
f:id:Infoment:20200305232657p:plain

しかしそのどれもが、気分的に何だかしっくり来ないことがある。
そんな時は、自分なりのイケてるスタイルを作ってしまおう。

※好みには個人差があるため、以下のスタイルはちょっと・・・という方は、お好みのスタイルに脳内変換してください。

私の今の気分で言えば、例えばこんな感じだ。
【既存のスタイル】
f:id:Infoment:20200305233123p:plain

【オリジナル】
f:id:Infoment:20200305233650p:plain

  • 線は極力引かない。脳に勝手に補助線を引いてもらう。
  • 色は極力、原色を使用しない。
  • カラー印刷でも白黒印刷でも、極力同じ見た目になる色を選択する。

ちなみに、新たなスタイルを作成するときマクロの記録をすると、こんなコードになった。

Sub Macro7()
    ActiveWorkbook.TableStyles.Add ("テーブル スタイル 2")
    With ActiveWorkbook.TableStyles("テーブル スタイル 2")
        .ShowAsAvailablePivotTableStyle = False
        .ShowAsAvailableTableStyle = True
        .ShowAsAvailableSlicerStyle = False
        .ShowAsAvailableTimelineStyle = False
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Font
        .TintAndShade = 0.249946592608417
        .ThemeColor = xlThemeColorLight1
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Borders(xlEdgeTop)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlHairline
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Borders(xlEdgeBottom)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlHairline
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Borders(xlEdgeLeft)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Borders(xlEdgeRight)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Borders(xlDiagonalDown)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Borders(xlInsideVertical)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlWholeTable) _
        .Borders(xlInsideHorizontal)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Interior
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -4.99893185216834E-02
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Borders(xlEdgeTop)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlHairline
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Borders(xlEdgeBottom)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = xlNone
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Borders(xlEdgeLeft)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Borders(xlEdgeRight)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Borders(xlDiagonalDown)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Borders(xlInsideVertical)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
    With ActiveWorkbook.TableStyles("テーブル スタイル 2").TableStyleElements(xlHeaderRow). _
        Borders(xlInsideHorizontal)
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        .LineStyle = 0
    End With
End Sub


これでは見づらいので少し整理すると、例えばこうなる。

Sub Sample()
    Dim TableStyle As Excel.TableStyle
    Set TableStyle = ActiveWorkbook.TableStyles.Add("New_Style_1")
        TableStyle.ShowAsAvailableTableStyle = True

    With TableStyle.TableStyleElements(xlWholeTable).Font
        .TintAndShade = 0.25
        .ThemeColor = xlThemeColorLight1
    End With
    
    ' 全体の罫線の設定。
    With TableStyle.TableStyleElements(xlWholeTable).Borders
        ' 内部の罫線を描画しない。
        .Item(xlInsideVertical).LineStyle = xlNone
        .Item(xlInsideHorizontal).LineStyle = xlNone
        
        ' 上と下にだけ線を追加。
        With .Item(xlEdgeTop)
            .ThemeColor = xlThemeColorDark1
            .Weight = xlThin
            .TintAndShade = -0.35
        End With
        
        With .Item(xlEdgeBottom)
            .ThemeColor = xlThemeColorDark1
            .Weight = xlThin
            .TintAndShade = -0.35
        End With
    End With
    
    ' ヘッダー行の設定。
    With TableStyle.TableStyleElements(xlHeaderRow)
        ' 塗り潰し設定。
        .Interior.ThemeColor = xlThemeColorDark1
        .Interior.TintAndShade = -0.05

        ' 罫線の設定。
        .Borders.Item(xlEdgeBottom).Weight = xlThin
    End With
End Sub

自分なりのお気に入りスタイルを作成し、規定値として設定するもよし、マクロで毎回作成するもよし。どうせ作るなら、成果物は格好良くしよう。
※ただし、所詮は独善な訳だから、他者への押しつけはダメよ。

以上、参考まで。