テキストボックスを作成する(AddTextboxメソッド)|Excel VBA |
新しいテキストボックスを作成します。
構文 Object.AddTextbox(Orientation, Left, Top, Width, Height)
| 設定項目 | 内容 |
|---|---|
| Object | Shapesコレクションオブジェクト [省略不可] |
| Orientation | テキスト配置の方向。 msoTextOrientationHorizontal(水平方向)、msoTextOrientationVertical(垂直方向)など MsoTextOrientation列挙体の値を指定[省略不可] |
| Left | テキストボックスの左端位置を指定 [省略不可] |
| Top | テキストボックスの上端位置を指定 [省略不可] |
| Width | テキストボックスの幅を指定 [省略不可] |
| Height | テキストボックスの高さを指定 [省略不可] |
次のサンプルは、数式の入力されているセルの横に、数式の内容をテキストとして持つテキストボックスを作成します。
Sub Sample()
Dim c As Range
For Each c In ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas)
'---数式が入力されているセルの横にテキストボックスを作成し、選択する
ActiveSheet.Shapes.AddTextbox( _
Orientation:=msoTextOrientationHorizontal, _
Left:=c.Offset(0, 1).Left, _
Top:=c.Offset(0, 1).Top, _
Width:=c.Offset(0, 1).Width, _
Height:=c.Offset(, 1).Height).Select
With Selection
.Characters.Text = c.FormulaLocal '---テキストに数式の内容を設定
.AutoSize = True '---自動サイズ調整にする
End With
c.Select '---テキストボックス選択を解除
Next c
End Sub