3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Excel VBAでグラフのプロットエリアの隅に凡例を配置する

Posted at

はじめに

Excelで作成したグラフの凡例の位置には、デフォルトで以下の選択肢があります。

  • グラフに重ねる/グラフに重ねない
  • 上/下/左/右/(右上)

自分は現在Excel2019を使用しているのですが、いつからか右上という選択肢が増えていました。
昔は無かったような気がします。

上下左右を選択する場合は、基本的にグラフに重ねず使用すると思います。
(グラフに重ねて上下左右を選択すると、見栄えが相当悪くなります。)
グラフに重ねる場合は、凡例をプロットエリア内の好きな位置に配置したい時でしょう。
そう、できればこんな感じに…

image.png

しかし、なぜかExcelにはこのようにプロットエリアの隅に凡例を配置するボタンが有りません。
ドラッグ&ドロップで配置するのはとても腕が疲れます。
では、マクロを使えばこのような配置が自動でできるようになるのでしょうか。

凡例の位置を指定する

Excel VBAを使えば、凡例の位置を指定することができます。

Sub test()
    With ActiveChart
        .Legend.Top = 50
        .Legend.Left = 50
    End With
End Sub

例えばこんなプログラムをグラフを選択して実行すれば、凡例の位置はこのようになります。

image.png

つまり、プロットエリアの隅の座標さえ求まれば、なんとかなりそうな気がします。

プロットエリアの隅の座標を求める

調べてみると、意外とすぐにそれらしいものが見つかりました。
こんな感じのプログラムです。

Sub test()
    With ActiveChart
        .Legend.Top = .PlotArea.Top
        .Legend.Left = .PlotArea.Left
    End With
End Sub

プロットエリアのTopとLeft…これでええやん!と思い実行すると…

image.png

違う。これじゃない。
どうやらExcelでは、"PlotArea"は軸の数値まで含めた部分になるようです。
さらに調べていくと、それらしいものが見つかります。

Sub test()
    With ActiveChart
        .Legend.Top = .PlotArea.InsideTop
        .Legend.Left = .PlotArea.InsideLeft
    End With
End Sub

これを実行すると…

image.png

やっとうまくいきました。
どうやら、グラフが描画される範囲は、”.PlotArea.Inside”となっているようです。
紛らわしいので、この部分をここではそのまま”プロットエリア内部”と呼びます。
これを応用すれば、いろんな位置に凡例を置けそうです。
例えば、左下に置きたい場合は、左上の位置からプロットエリア内部の幅だけ下げて、
凡例の幅だけ上げれば良さそうです。
(”.PlotArea.InsideDown”みたいなのも一応打ってみましたがだめでした。)

Sub test()
    With ActiveChart
        .Legend.Top = .PlotArea.InsideTop
        .Legend.Left = .PlotArea.InsideLeft + .PlotArea.InsideWidth - .Legend.Width
    End With
End Sub

image.png

さらに、プロットエリア内部の右側に寄せたいときも同様にできます。
最後にまとめとして4パターン載せておきます。

Sub test()
    With ActiveChart
        'プロットエリア内左上側
        .Legend.Top = (.PlotArea.InsideTop)
        .Legend.Left = (.PlotArea.InsideLeft)
        'プロットエリア内右上側
        .Legend.Top = (.PlotArea.InsideTop)
        .Legend.Left = (.PlotArea.InsideLeft + .PlotArea.InsideWidth - .Legend.Width)
        'プロットエリア内左下側
        .Legend.Top = (.PlotArea.InsideTop + .PlotArea.InsideHeight - .Legend.Height)
        .Legend.Left = (.PlotArea.InsideLeft)
        'プロットエリア内右下側
        .Legend.Top = (.PlotArea.InsideTop + .PlotArea.InsideHeight - .Legend.Height)
        .Legend.Left = (.PlotArea.InsideLeft + .PlotArea.InsideWidth - .Legend.Width)
    End With
End Sub

感想

これらがデフォルトで使用できるのはいつになるのでしょうか?
もう少しこういった機能を充実させてくれれば使いやすくなる気がします。
とはいえ、やろうと思えばだいたいできてしまうのものです。
これで、ボタン1つで凡例の位置を自由自在に…

※グラフの大きさを変えたときは位置がずれてしまうので、
 早くデフォルトの設定にプロットエリア内部の隅を追加してください。早く。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?