将形状复制并粘贴到另一个工作表,并使其适应单元格大小

时间:2016-03-15 10:12:18

标签: excel vba excel-vba shape shapes

通过在同一个Excel文件中复制和粘贴形状,我遇到了问题。

我在一个Excel文件中有两个表格,在表格中#34; Tabelle 1"只有一张图片,我假设自动获取名称为"图片1"。我想复制这张图片并将其粘贴到名为"概述"与#34; Tabelle 1"在同一个Excel文件中。我想将此图片粘贴到单元格A1,我希望它适合单元格大小,并且不要大于单元格A1。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

Dim pasteCell As Range

Set pasteCell = Sheets("Overview").Range("A1")

Sheets("Tabelle1").Shapes("Picture1").Copy
Sheets("Overview").Paste pasteCell

Sheets("Overview").Shapes(1).Height = pasteCell.Height
Sheets("Overview").Shapes(1).Width = pasteCell.Width

假设概述表上没有图像。

答案 1 :(得分:0)

Option Explicit

Sub Copy_Shape()

Dim Sh1 As Worksheet
Dim Sh2 As Worksheet
Dim PasteCell As Range
Dim Pic1 As Shape
Dim Pic2 As Shape

With ThisWorkbook
    Set Sh1 = .Sheets("Tabelle 1")
    Set Sh2 = .Sheets("Overview")
End With


Set Pic1 = Sh1.Shapes("Picture 1")

Pic1.Copy

With Sh2
    .Paste
    Set Pic2 = .Shapes(.Shapes.Count)
    Set PasteCell = .Cells(1, 1)
End With

With Pic2
    .Height = PasteCell.Height
    .Width = PasteCell.Width
    .Top = PasteCell.Top
    .Left = PasteCell.Left
End With

Set Pic1 = Nothing
Set Pic2 = Nothing
Set PasteCell = Nothing
Set Sh1 = Nothing
Set Sh2 = Nothing

End Sub

如果你是VBA的新手,这是练习的第一个好习惯。