• ベストアンサー

マクロで、選択セルの直近にオブジェクト(オートシェイプ)を作成したい

お世話になります。エクセルマクロのまだまだ初心者です。 エクセルで、5列×25行ほどのセル範囲における作業ですが、そのなかのいくつかに文字が入力されています。どこのセルにいくつ入力されるかは常に変化します。その、入力されている全てのセルのすぐ近くに、一定の大きさのオートシェイプ(吹き出し)を作成したいのです。 入力されているセルを選択することと、オートシェイプを作るマクロは、 Range("A1:D25").Select Selection.SpecialCells(xlCellTypeConstants, 2).Select ’吹き出しの作成 ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangularCallout, 165#, 61.5, _ 105.75, 56.25).Select Selection.Characters.Text = "Hello World!" With Selection.Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 9 End With With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlCenter .ReadingOrder = xlContext .Orientation = xlHorizontal End With こんな感じに作れましたが、選択セルのすぐ近くという位置取りがどう調べても分かりません。 位置は、セルの上下左右どこでもいいのですが、そのセルを説明するように吹き出しを入れたいのです。 良い方法があるでしょうか。 よろしくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • mitarashi
  • ベストアンサー率59% (574/965)
回答No.3

#1です。鏡餅状態になりますが、適宜調整してください。 Sub Macro3() Dim myCell As Range, targetRange As Range If TypeName(Selection) <> "Range" Then Exit Sub Set targetRange = Selection For Each myCell In targetRange ActiveSheet.Shapes.AddShape(msoShapeOvalCallout, myCell.Left + myCell.Width * 1.5, myCell.Top + myCell.Height / 2, 70, 25). _ Select Selection.Characters.Text = "Hello World!" Selection.Font.Size = 8 Selection.ShapeRange.Adjustments.Item(1) = -0.5 Selection.ShapeRange.Adjustments.Item(2) = 0 Next myCell End Sub

suikaman
質問者

お礼

すごいです。完璧です。 早く自分でもこのように記述できるようになりたいです。どのように勉強されたのでしょうか。できればアドバイス下さい。 本当に、ありがとうございました。

その他の回答 (2)

  • imogasi
  • ベストアンサー率27% (4737/17068)
回答No.2

シートのSelectionChangeイベントに(ただしお勧めは後記) Private Sub Worksheet_SelectionChange(ByVal Target As Range) l = Target.Left + Target.Width + 10 t = Target.Top + 10 w = 100 h = 60 ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangularCallout, l, t, _ w, h).Select Selection.ShapeRange.Adjustments.Item(1) = -0.1716 Selection.Characters.Text = "Hello World!" With Selection.Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 9 End With With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlCenter .ReadingOrder = xlContext .Orientation = xlHorizontal End With Selection.ShapeRange.Adjustments.Item(1) = -0.1 Selection.ShapeRange.Adjustments.Item(2) = -0.1 End Sub l,t,w,hの値は適当に試行錯誤で修正のこと。 Adjustments.Item(1) Adjustments.Item(2) のことは私も良くわかってない。シェイプを選択したとき現れる黄色小四角の位置を決めるものと思っている。 (1)のマイナスは左へ修正、 (2)のマイナスは上へ、 を示すようだ。 ーーーーーー SelectionChangeイベントはシートをアクチブにしてセルを選択すると すぐ効果が始まり、うっとうしい。またいつまでも続いてしまう。 だからトリガーとなるボタンをシートに1つ設けて、そのClickイベントに上記の中身を入れるのが良さそう。 その場合TargetはSelectionに書き直すこと。 Private Sub CommandButton1_Click() Set Target = Selection l = Target.Left + Target.Width + 10 t = Target.Top + 10 w = 100 h = 60 ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangularCallout, l, t, _ w, h).Select Selection.ShapeRange.Adjustments.Item(1) = -0.1716 Selection.Characters.Text = "Hello World!" With Selection.Font .Name = "MS Pゴシック" .FontStyle = "標準" .Size = 9 End With With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlCenter .ReadingOrder = xlContext .Orientation = xlHorizontal End With Selection.ShapeRange.Adjustments.Item(1) = -0.1 Selection.ShapeRange.Adjustments.Item(2) = -0.1 End Sub

suikaman
質問者

補足

早速のご回答ありがとうございます。 教えていただいたものが、ほぼ目的のものですが、残念ながらオートシェイプが一つしかできません。私は、選択範囲で選択された複数のセルすべてに付けたいのです。 SelectionChangeイベントに入れるのも、ご指摘のとおり少々難があります。 再度、ご教示いただけないでしょうか。

  • mitarashi
  • ベストアンサー率59% (574/965)
回答No.1

吹き出しの種類が違うかもしれませんが、ご参考まで。位置の決め方はあくまで参考ですので、お好きにアレンジしてください。 Dim myCellを宣言して使っているのは、インテリセンス機能が、オブジェクト(ここではセル)がどの様な属性を持っているのか表示してくれる様にしているものです。手で打ち込んでもらえると、ご理解いただけると思います。Adjustments.Itemのところは試行錯誤で入れた数字です。 Sub Macro2() Dim myCell As Range Set myCell = ActiveCell ActiveSheet.Shapes.AddShape(msoShapeOvalCallout, myCell.Left + myCell.Width * 1.5, myCell.Top + myCell.Height / 2, 70, 25). _ Select Selection.Characters.Text = "Hello World!" Selection.Font.Size = 8 Selection.ShapeRange.Adjustments.Item(1) = -0.5 Selection.ShapeRange.Adjustments.Item(2) = 0 End Sub

suikaman
質問者

補足

早速のご回答ありがとうございます。 教えていただいたものが、ほぼ目的のものですが、残念ながらオートシェイプが一つしかできません。私は、選択範囲の選択セルすべてに付けたいのです。 Set myCell = ActiveCell のところを、Selectionに変えてみましたができません。 再度、ご教示いただけないでしょうか。

関連するQ&A

  • エクセルのマクロ セルの結合プロシージャを教えてください。

    マクロの記憶でのプロシージャを Rangeを変数型にしたいのです。 行も列も定めまずに、範囲はA1:BX45です。 Offsetを使うのか、もう何がなんだかわからないので 教えてください!! マクロの記憶でのプロシージャです。 ↓ Keyboard Shortcut: Ctrl+d ' End Sub Range("R26:T27").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = True .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge End Sub

  • 任意のセルでマクロを実行させたい

    アクティブセルにマクロを実行させたいのですがうまくいきません。 2007のエクセルを使用しています。 (1)命令文で指定しているセル(G9:G11)をJ9:J11やR14:R16等でも使用したい。 (2)また作成したマクロを同シート内オートシェイプに登録したい。 よろしくお願いいたします。 Sub Macro2() ' ' Macro2 Macro ' ' Range("G9:G11").Select Selection.ClearContents With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlTop .Orientation = xlVertical .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With With Selection.Font .Name = "MS P明朝" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 16777164 .TintAndShade = 0 .PatternTintAndShade = 0 End With ActiveCell.FormulaR1C1 = "搬入" With ActiveCell.Characters(Start:=1, Length:=2).Font .Name = "MS P明朝" .FontStyle = "標準" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic .TintAndShade = 0 .ThemeFont = xlThemeFontNone End With ActiveCell.Characters(1, 2).PhoneticCharacters = "ハンニュウ" Range("G12").Select End Sub

  • マクロで隣接する上下のセルを比較後、処理をするには

    よろしくお願いします。 Excel2007です。 A1からA5000までデータが入っています。 データは文字列です。 その文字列を上から順に比較していき、 隣接する上下のデータが一致した場合、 さらにその下が一致しているかを調べ、 その作業を一致しなくなるまで続けます。 最後に、一致した部分すべてを選択し、 セルをまとめて結合し、左寄せしたいのです。 まとめて結合し、左寄せ、という部分は、 マクロを記録し、以下のようにするのはわかったのですが、 Range("a4123:a4131").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge With Selection .HorizontalAlignment = xlLeft .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With そして、これをa5000(データが格納されている最後のセル)まで 続けたいのです。 例えば、 A1とA2を比較し、一致しないなら、A2とA3を比較。 一致したら、さらにA2とA4が一緒かどうか比較。 一致が無くなるまで続けて、最後に処理。 という感じです。 前半の部分が全くわかりません。 ご教示願えませんでしょうか。よろしくお願いします。

  • エクセル2010のマクロについて、セル結合の解除

    全てのセルを選択して、結合されているセルがあったら全て解除したいのですが、 マクロの記録で作成すると、↓を何百回も繰り返すソースになってしまいます With Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlCenter .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With 長すぎるとエラーになってしまいますし、何回繰り返すかもランダムなので、↑の処理を、 結合されたセルがなくなるまで繰り返すという設定にしたいのですが、可能でしょうか? よろしくおねがいします。

  • エクセルマクロで教えてください

    エクセル2003です。 自動マクロで下記のようなマクロを造ったんですが Selection.End(xlDown).Select   Range("A29:D29").Select  ■A29を止まったセルの番号にしたいのです。(A**からD**まで)     With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("A30").Select ■A30を止まったセルの番号にしたいのです 以上2箇所の指定を教えていただけますでしょうか。 よろしくお願いいたします。

  • 【自作マクロ】いらない部分を削除していただきたい。

    自分で行ってみたマクロですが、長く、見づらいです。 いらない部分を削除していただける方がいましたら、お願いいたします。 作業としては、 /////////////////////////////////// あああ いいい ううう えええ おおお かかか      あかさたなはまやらわん の、「あかさたなはまやらわん」を削除し、セルの結合を解除し、 あああ、いいい など文字のあるセルと下のセルを結合して格子をつける。 /////////////////////////////////// Sub 項目を1行にして摘要を削除する() ' ' 項目を1行にして摘要を削除する Macro ' ' Range("C7:H7").Select ActiveCell.FormulaR1C1 = "" Range("C7:H7").Select With Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With Selection.UnMerge Range("C6:C7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("D6:D7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("E6:E7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("F6:F7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("G6:G7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("H6:H7").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("C6:H7").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 With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With End Sub

  • オートシェイプの位置

    エクセルですが。 セレクトされているオートシェイプを所定の位置に配置するマクロとして、以下は動作するのですが。 Selection.ShapeRange.Left = 250 Selection.ShapeRange.Top = 100 名前ボックスからオートシェイプの名前を aaa に変更し、以下のマクロを実行すると、いずれもエラーになってしまいます。 aaa.ShapeRange.Left = 250 aaa.ShapeRange.Top = 100 aaa.Left = 250 aaa.Top = 100 どこが間違っているのでしょうか?

  • オートシェイプの幅を操作するには?

    コマンドボタンにマクロを登録して、オートシェイプの幅を操作したいのですが、 とあるHPから Sub WIDTH_ADD() Selection.ShapeRange.Width = Selection.ShapeRange.Width + 1 End Sub というマクロを見付けました。 ただ、これでは1ずつしか広がりません。 決まったセルに入れた数字分、増加させるにはどのようにしたらよいのでしょうか? 例えば セルA1に10と入力すれば、10増えると言う具合にです。 もしくは、増減させるのではなく幅にあたる数値をセルに入力することによって 幅を自由に変更する方法はありませんでしょうか? 良い方法がありましたらお願いします。

  • オートシェイプに関するマクロ

    エクセルのオートシェイプの書式設定のサイズの倍率(縦横比を固定)を、120%にするマクロって可能でしょうか? 沢山の図形の大きさを調節する作業があるのですが、その都度オートシェイプの書式設定を開いて値を入力すると大変な手間になってしまいます。 図形をセレクトしてマクロを実行するだけで、大きさを変化させるようにしたいのですが。 ご存知の方がいましたら、よろしくお願いします。

  • セル結合と列挿入

    マクロの記録を使い書いて見ましたが、1行置きに3行挿入し 、A2:A5 , B2:B5 ,C2:C5 ,D2,D5 言う感じでセル結合を5000行まで行い、最後にD、E列、列の挿入したいのですが、どのように書けば宜しいでしょうか? すでに、データが入っています。 Sub Macro1() Rows("3:5").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Rows("7:9").Select Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove Range("A2:A5").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("B2:B5").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("C2:C5").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("A6:A9").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("B6:B9").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Range("C6:C9").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = True .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge Columns("D:E").Select Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove Selection.ColumnWidth = 18.88 Range("A2:A5").Select End Sub

専門家に質問してみよう