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
  • 実行結果
    hatena_20040621_1.jpg

添付ファイル: filehatena_20040621_1.jpg 587件 [詳細] filehatena_20040621.jpg 302件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2021-07-13 (火) 21:53:53 (1008d)