「マクロの記録」の結果の、要らないところを消してみる

ふと思い立って、「マクロの記録」の結果の、要らないところを消してみた。
f:id:Infoment:20190730220853p:plain

まず記録したのがこちら。

  • 真っ新なA1セルの外周に罫線を引く

f:id:Infoment:20190730221445p:plain

記録された結果がこちら。四方に線を引くだけなのに、なんでこんなに長ったらしいんだ?と、きっと、誰もが一度は思ったに違いない(憶測)。

Sub Macro6()
    Range("A1").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
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
End Sub

まず、よくあるのがSelectとSelectionの関係。

  1. 〇〇を選ぶ
  2. 選んだものを△△する

であれば、「選ぶ」と「選んだもの」を約分(?)するかの如く消せる。

  1. 〇〇を△△する

とりあえず、Selectionを全てRange("A1")に置き換える。また、選ぶ必要が無くなったので、一行目の「Range("A1").Select」は削除する。

Sub Macro6_()
    Range("A1").Borders(xlDiagonalDown).LineStyle = xlNone
    Range("A1").Borders(xlDiagonalUp).LineStyle = xlNone
    With Range("A1").Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1").Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1").Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1").Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("A1").Borders(xlInsideVertical).LineStyle = xlNone
    Range("A1").Borders(xlInsideHorizontal).LineStyle = xlNone
End Sub

次いで、↓ ここに着目する。

    Range("A1").Borders(xlDiagonalDown).LineStyle = xlNone
    Range("A1").Borders(xlDiagonalUp).LineStyle = xlNone

セル内の斜め線、右下がりと右上がりが「無い」(xlNone)と言っている。元から無いのに、改めて無いという必要は無い(ややこしい)。

同じような理由から、↓ ここにも着目する。

    Range("A1").Borders(xlInsideVertical).LineStyle = xlNone
    Range("A1").Borders(xlInsideHorizontal).LineStyle = xlNone

今回は単一セルであるため、中身の縦横線を気にする必要が無い。
f:id:Infoment:20190730222027p:plain

以上の理由から、上記4行をバッサリと消してしまおう。

Sub Macro6_()
    With Range("A1").Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1").Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1").Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Range("A1").Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
End Sub

続いて、この部分に着目する。

        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin

例えば線種(LineStyle)だけ選んだら、残りってどうなるんだろう?
試してみた。

Sub Macro7()
    Range("C3").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Debug.Print Range("C3").Borders(xlEdgeBottom).ColorIndex
    Debug.Print Range("C3").Borders(xlEdgeBottom).TintAndShade
    Debug.Print Range("C3").Borders(xlEdgeBottom).Weight
End Sub

f:id:Infoment:20190730223718p:plain

どうやら、線の太さの初期値は「xlThin=2」らしい。つまり、改めて指定する必要は無いわけだ。ColorIndexの値が違うが、試しに思い切って、線種以外のプロパティ指定を全部消してしまおう。

Sub Macro6_()
    Range("A1").Borders(xlEdgeLeft).LineStyle = xlContinuous
    Range("A1").Borders(xlEdgeTop).LineStyle = xlContinuous
    Range("A1").Borders(xlEdgeBottom).LineStyle = xlContinuous
    Range("A1").Borders(xlEdgeRight).LineStyle = xlContinuous
End Sub

かなり短くなった。当初の面影は、ほとんどない。
それにしても上下左右、一本ずつ指定するのはとても面倒くさい。一度に指定できないものか。

ということで、ダメで元々、こんな風に書いてみた。

Sub Macro6_()
    Range("A1").Borders.LineStyle = xlContinuous
End Sub

実行した結果がこちら。
f:id:Infoment:20190730224825g:plain

何だか、上手くいったっぽい。

ということで、最終的には一行にまでダイエットすることができた。
ただし今回は「消す」ことを主目的としたため、消し方がかなり荒っぽくなっている。実際に省略する場合は、可否をよく確認してから行いましょう。

参考まで。