将形状从一张纸复制并粘贴到另一张纸上

时间:2020-06-18 15:29:46

标签: copy paste shapes

有人可以告诉我为什么这个excel VBA将形状从一张纸复制并粘贴到另一张纸会失败吗?我的工作簿中存在形状“ StandingsPix”和工作表“ Pictures”,实际上,当我录制一个宏以手动执行此操作时,它会创建类似的代码。

通过“未设置对象变量”分配“ p”失败。非常感谢您的帮助。

Sub CopyPictureToScorecard(TargetCells As Range)

Dim p As Shape
Dim p2 As Shape
Dim TargetWS As Worksheet

    Set TargetWS = Sheets("Scorecards")

    p = Sheets("Pictures").Shapes.Range(Array("StandingsPix"))      <== Fails Here

    p.Copy
    TargetWS.Paste

    'make sure the picture is properly centered on the scorecard
    Set p2 = TargetWS.Shapes(TargetWS.Shapes.Count)

            p2.Width = p.Width
            p2.Height = p.Height
            p2.Top = TargetCells.Top + (TargetCells.Height / 2) - (p2.Height / 2)
            p2.Left = TargetCells.Left + (TargetCells.Width / 2) - (p2.Width / 2)
            p2.Line.Visible = False

End Sub

当我手动记录一个宏(成功!)时,这是它生成的代码:

Sheets("Pictures").Select
ActiveSheet.Shapes.Range(Array("StandingsPix")).Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Scorecards").Select
Range("K24").Select
ActiveSheet.Paste
Selection.ShapeRange.IncrementLeft 621
Selection.ShapeRange.IncrementTop -369.75

0 个答案:

没有答案