如何使用VBA在Excel中裁剪选定的图片

时间:2015-12-05 01:05:07

标签: excel vba

我正在尝试在Excel中裁剪选定的图片,并尝试使用此代码:

Sub CropPicture()

    Dim shpCrop As Shape
    Dim sngMemoLeft As Single
    Dim sngMemoTop As Single

    Set shpCrop = Selection

    With shpCrop
        sngMemoLeft = .Left
        sngMemoTop = .Top
        With .PictureFormat
            .CropLeft = 10
            .CropTop = 10
            .CropBottom = 10
            .CropRight = 10
        End With
        .Left = sngMemoLeft
        .Top = sngMemoTop
    End With

End Sub

但是,这给了我一个类型不匹配的运行时错误。问题是这部分代码:     设置shp =选择

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

尝试

Set shpCrop = ActiveSheet.Shapes(Selection.Name)

如果您在“立即”窗格中尝试? typename(selection),则会获得“#34;图片"”,但您可以通过其名称属性获取形状。