Excel 関連情報 †
サンプルコード †
オートシェイプの使い方 †
- オートシェイプを使って線を描画するサンプル(その1)
' オートシェイプを使って線を描画するサンプル
' <テストデータ>
' 番号, X軸, Y軸, 経路フラグ(-1の場合、経路なし)
' 1,9,3,-1
' 2,4,7,1
' 3,10,8,-1
' 4,13,5,1
' 5,8,15,1
Sub LineDrawTest()
Const nCount = 5 ' データ件数
Const nScale = 10 ' 倍率
Dim n ' データの番号
Dim x_st ' 線の開始位置(X軸)
Dim y_st ' 線の開始位置(Y軸)
Dim x_ed ' 線の終了位置(X軸)
Dim y_ed ' 線の終了位置(Y軸)
Dim i ' ループカウンタ
Dim flag ' 経路フラグ
Dim line ' オートシェイプ:線
Dim oval ' オートシェイプ:円
Dim text ' オートシェイプ:テキストボックス
For i = 1 To nCount
n = ActiveSheet.Cells(i, 1)
x_ed = ActiveSheet.Cells(i, 2) * nScale
y_ed = ActiveSheet.Cells(i, 3) * nScale
flag = ActiveSheet.Cells(i, 4)
' flag が -1 の場合、経路が無いものとします。
If i = 1 Or flag = -1 Then
x_st = x_ed
y_st = y_ed
Else
End If
With ActiveSheet
' 線を表示(X座標、Y座標、X座標2、Y座標2)
Set line = .Shapes.AddLine(x_st, y_st, x_ed, y_ed)
' 点(○)を表示(X座標、Y座標、幅、高さ)
Set oval = .Shapes.AddShape(msoShapeOval, x_ed - 2, y_ed - 2, 5, 5)
oval.Fill.ForeColor.SchemeColor = 10 ' 色(赤)を指定
' 番号を表示(X座標、Y座標、幅、高さ)
Set text = .Shapes.AddTextbox(msoTextOrientationHorizontal, x_ed, y_ed, 15, 15)
text.Select
Selection.Characters.text = CStr(n)
text.Fill.Visible = msoFalse ' 非表示に
text.line.Visible = msoFalse ' 非表示に
End With
x_st = x_ed ' 次回開始位置を指定
y_st = y_ed ' 次回開始位置を指定
Next
End Sub
- 実行結果
|