首页 > 解决方案 > Excel中的VBA自动更新图像尺寸

问题描述

编辑:17-07-2018 找到了在 Excel 中检索图像尺寸的解决方案。

我已经创建了一个代码来在 Excel 中检索图像文件并且它工作正常,但是一旦我调整了图像的大小,它就不会自动更新它的值,我需要在之前的图像之间切换,然后返回到调整大小的图像以获取值.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim mypic As Picture

   If Target.Address = "$A$4" Then
        Me.Pictures.Visible = False
        With Range("e2")
            For Each mypic In Me.Pictures
                If mypic.Name = .Text Then
                    mypic.Visible = True
                    mypic.Top = .Top
                    mypic.Left = .Left
                    Exit For
                End If
            Next mypic
        End With

        With ActiveSheet
            s = Round(.Shapes(.Range("e2").Value).Height / 72 * 2.54, 2) & "cm"
            y = Round(.Shapes(.Range("e2").Value).Width / 72 * 2.54, 2) & "cm"


        MsgBox "Picture dimensions are " & vbLf & vbLf & _
            "Height: " & s & vbLf & vbLf & _
            "Width: " & y

            .Range("Q5") = s
            .Range("Q6") = y

        End With
    End If
End Sub

上面的代码有一种方法可以自动更新值,而无需关闭 Excel 文件或在图像之间切换。

提前谢谢你!

标签: vbaexcel

解决方案


推荐阅读