実現したいこと
VBAにて、作成したグラフの上にテキストボックスを挿入したいです
前提
VBAにて、作成したグラフの上にテキストボックスを挿入したいです。
テキストボックス自体は挿入、貼り付けが出来るのですが、挿入した後、グラフ全体を動かすとテキストボックスとグラフが離れてしまいます。
また、VBAですが、マクロの記録にて作成したため、.selectなど直接グラフの操作部分も含まれています。
該当のソースコード
VBA
1 With Selection.Format.TextFrame2.TextRange.Characters(1, 5).Font 2 .BaselineOffset = 0 3 .Bold = msoFalse 4 .NameComplexScript = "+mn-cs" 5 .NameFarEast = "+mn-ea" 6 .Fill.Visible = msoTrue 7 .Fill.ForeColor.RGB = RGB(89, 89, 89) 8 .Fill.Transparency = 0 9 .Fill.Solid 10 .Size = 10 11 .Italic = msoFalse 12 .Kerning = 12 13 .Name = "+mn-lt" 14 .UnderlineStyle = msoNoUnderline 15 .Strike = msoNoStrike 16 End With 17 With Selection.Format.TextFrame2.TextRange.Characters(6, 4).Font 18 .BaselineOffset = 0 19 .Bold = msoFalse 20 .NameComplexScript = "+mn-cs" 21 .NameFarEast = "+mn-ea" 22 .Fill.Visible = msoTrue 23 .Fill.ForeColor.RGB = RGB(89, 89, 89) 24 .Fill.Transparency = 0 25 .Fill.Solid 26 .Size = 10 27 .Italic = msoFalse 28 .Kerning = 12 29 .Name = "+mn-lt" 30 .UnderlineStyle = msoNoUnderline 31 .Strike = msoNoStrike 32 End With 33 ActiveChart.ChartArea.Select 34 ActiveChart.Axes(xlCategory).MinorGridlines.Select 35 With Selection.Format.Line 36 .Visible = msoTrue 37 .Weight = 1 38 End With 39 With Selection.Format.Line 40 .Visible = msoTrue 41 .ForeColor.ObjectThemeColor = msoThemeColorBackground1 42 .ForeColor.TintAndShade = 0 43 .ForeColor.Brightness = 0 44 .Transparency = 0 45 End With 46 ActiveChart.Axes(xlValue).MajorGridlines.Select 47 With Selection.Format.Line 48 .Visible = msoTrue 49 .ForeColor.ObjectThemeColor = msoThemeColorBackground1 50 .ForeColor.TintAndShade = 0 51 .ForeColor.Brightness = 0 52 .Transparency = 0 53 End With 54 With Selection.Format.Line 55 .Visible = msoTrue 56 .Weight = 1 57 End With 58 ActiveChart.Axes(xlValue).Select 59 ActiveChart.Axes(xlValue).MinimumScale = -100 60 ActiveChart.Axes(xlValue).MaximumScale = 0 61 ActiveChart.FullSeriesCollection(1).Select 62 With Selection.Format.Line 63 .Visible = msoTrue 64 .ForeColor.RGB = RGB(255, 255, 0) 65 .Weight = 1 66 .Transparency = 0 67 End With 68 69'ここからテキストボックスの作成 70 ActiveChart.ChartArea.Select 71 72 Application.CommandBars("Format Object").Visible = False 73 74 ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 1350.6, 500.4, _ 75 103.2, 19.2).Select 76 With Selection.ShapeRange.Fill 77 .Visible = msoTrue 78 .ForeColor.ObjectThemeColor = msoThemeColorText1 79 .ForeColor.TintAndShade = 0 80 .ForeColor.Brightness = 0 81 .Transparency = 0 82 .Solid 83 End With 84 Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _ 85 "2023/10/31/15:21" 86 Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 16).ParagraphFormat. _ 87 FirstLineIndent = 0 88 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 16).Font 89 .NameComplexScript = "+mn-cs" 90 .NameFarEast = "+mn-ea" 91 .Fill.Visible = msoTrue 92 .Fill.ForeColor.RGB = RGB(0, 112, 192) 93 .Fill.Transparency = 0 94 .Fill.Solid 95 .Size = 14 96 .Name = "+mn-lt" 97 End With 98 Selection.ShapeRange.ScaleWidth 1.1627906977, msoFalse, msoScaleFromBottomRight 99 Selection.ShapeRange.ScaleHeight 0.875, msoFalse, msoScaleFromTopLeft 100 Selection.ShapeRange.ScaleHeight 1.25, msoFalse, msoScaleFromTopLeft 101 102
試したこと
69行目辺りからテキストボックスの作成です。
テキストボックス挿入の前後のソースコードが関係するのではと思い、グラフのどこを選択してテキストボックスを作成しているか確認しましたが、調べたところ、「ActiveSheet.Shapes.AddTextbox」で出来るとありました。「ActiveSheet.Shapes.AddTextbox」でテキストボックスを作成しているのでグラフとくっつくと思ったのですが、上手くいきませんでした。
また、「ActiveSheet.Shapes.AddTextbox」の引数の値を変更してもテキストボックスの位置が変わるだけで、グラフ上には作成できませんでした。
間違っている箇所、アドバイスなど頂けたらありがたいです。
よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/11/01 12:13