【问题标题】:Excel VBA crop picture/shape and put in cellsExcel VBA裁剪图片/形状并放入单元格
【发布时间】:2017-10-02 15:48:14
【问题描述】:

我正在处理五张图片(打印屏幕形状)并希望将它们放置在选定的单元格中,例如。细胞 N21、P21、S21、U21 和 W21。我有下面的 VBA 代码来裁剪五张图片/形状,它工作正常,但我不确定如何将它们放在上面选定的单元格中。任何人都可以启发我吗?另外,如何用VBA按钮删除五张图片?

Sub CropPictures()
    Application.ScreenUpdating = False

    Dim shp As Shape 
    Dim sngMemoLeft As Single
    Dim sngMemoTop As Single
    Dim i As Integer, j As Integer

   i = 0: j = 0
   For Each shp In ActiveSheet.Shapes
       i = i + 1

       With shp
           If .Type = 13 Or .Type = 15 Then
               j = j + 1
               sngMemoLeft = .Left
               sngMemoTop = .Top
               With .PictureFormat
                   .CropLeft = 20
                   .CropTop = 195
                   .CropBottom = 27
                   .CropRight = 565
               End With
               With shp
                   .Height = 20
                   .Width = 195
               End With
               .Left = sngMemoLeft
               .Top = sngMemoTop
           End If
        End With
    Next shp
End Sub

【问题讨论】:

  • 您已经在使用.Left.Top 移动图片。如果您希望它们位于单元格内,则 does not happen.
  • 嗨,谢谢分享,现在裁剪图片进入我选择的单元格,我可以细化一个单元格范围并将它们一一放置吗?
  • 不要在另一个with shp 中使用with shp .... 删除内部with shp 和突出.height.width 以与.left 相同
  • 好的,谢谢分享

标签: excel vba


【解决方案1】:

您可以按照以下帖子pic into excel at specific cell 所示进行定位,这可以通过将形状的左上角属性设置为单元格引用来实现。

假设您要删除活动工作表中的所有形状,您可以按照以下方式放置一些内容:

Dim shp As Excel.shape

For Each shp In ActiveSheet.Shapes
    shp.Delete
Next

如果是特定的 5 个形状,您可以使用相同的结构,但测试形状是否具有特定名称,然后删除。

If shp.Name = "Shape1" Then 
        shp.Delete 
End If 

我也更喜欢明确命名我的图片,例如myPict.Name = "Pic1" 这避免了以后跟踪形状编号的问题。

【讨论】:

  • 非常感谢您为我指出正确的方向,将重新查看代码。
猜你喜欢
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 2017-06-14
  • 2019-05-20
  • 2017-07-05
  • 1970-01-01
相关资源
最近更新 更多