0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【VBA】画像の拡縮マクロ

Last updated at Posted at 2024-02-03

画像をクリックすると、画像が拡大/縮小するマクロです。
画像にこのマクロを登録して使います。
「代替テキスト」をフラグのように使って処理しています。

Sub GazouKakusyuku()
    With ActiveSheet.Shapes(Application.Caller)
        .LockAspectRatio = True '縦横比を固定する
        .ZOrder msoBringToFront '最前面に表示する

        '拡大処理
        If .AlternativeText = "" Then
            .AlternativeText = .Height '元の画像サイズを「代替テキスト」に退避しておく
            NextHeight = 1.5 * .Height '拡大後の画像サイズを求める
            
            eachStep = (NextHeight - .Height) / 20 '20段階で拡大する場合の1段階あたりの変化量
            
            For i = 1 To 20
                If i >= 20 Then
                    .Height = NextHeight
                    Exit For
                Else
                    .Height = .Height + eachStep
                End If
                Application.Wait [Now()] + 20 / 86400000 '20ミリ秒待つ
            Next i
        '縮小処理(※元の画像サイズに戻す)
        Else
            NextHeight = .AlternativeText
            
            eachStep = (.Height - NextHeight) / 20

            For i = 1 To 20
                If i >= 20 Then
                    .Height = .AlternativeText
                    Exit For
                Else
                    .Height = .Height - eachStep
                End If
                Application.Wait [Now()] + 20 / 86400000 '20ミリ秒待つ
            Next i
            .AlternativeText = ""
        End If
    End With
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?