如何在不同的 Sheet 中复制一个形状?
How can I duplicate a shape in a different Sheet?
我有一个 sprite sheet,其中包含国家/地区的国旗和代码,将国旗的形状放置在选择的每个单元格中,其中包含两个字母的国家/地区代码。
如果我从外部 .png 文件为每个单元格添加图片,这会很好地工作。
当我尝试将 .png 放入辅助 sheet 和 .Duplicate 标志中时,无论我尝试什么,都会在 spritesheet 所在的 sheet 中创建标志。下面的剥离示例
我怎样才能复制不同的形状 Sheet?
Dim sh,shf as shape
dim is as range
Dim ws As Worksheet: Set ws = ActiveSheet
Set shf = Worksheets("flags").Shapes("flags")
ws.Activate
...
For each i in Selection.Cells
'get country code and calculate offsets
Set sh = shf.Duplicate 'this puts the shape in the flags sheet
....
正如其他 answers 发现的那样,您不能将一个形状从一个 sheet 移动到另一个,.Duplicate
只会在同一个地方复制。
最简单的步骤可能只是 新作品的形状 sheet。在此过程中给它一个新名称。
Dim s1 As Worksheet, s2 As Worksheet
Dim shp2 As Shape
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("flags")
s2.Shapes(yourflag).copy
s1.Paste
set shp2 = s1.Shapes(s1.shapes.count)
如前所述,Duplicate
方法无法在不同的 sheet 中执行此操作。但是你可以通过下一种方式剪切和粘贴复制的形状(如果你喜欢复制而不使用经典的复制 - 粘贴解决方案):
Sub testShapeDuplicate()
Dim sh As Shape, shf As Shape, dLeft As Double, dTop As Double
Set shf = = Worksheets("flags").Shapes("flags")
Set sh = shf.Duplicate
sh.Name = "Dupl": dLeft = shf.left: dTop = shf.top
sh.Cut
With ActiveSheet.Next 'it will be moved in the next sheet. Use here what sheet you need, please
.Paste
With .Shapes(.Shapes.count)
.left = dLeft: .top = dTop 'put the shape in the same position
End With
End With
End Sub
除此之外,当您声明 Dim sh,shf as shape
时,只有 shf
对象被声明 As Shape
。 sh
声明为 As Variant
...
我有一个 sprite sheet,其中包含国家/地区的国旗和代码,将国旗的形状放置在选择的每个单元格中,其中包含两个字母的国家/地区代码。 如果我从外部 .png 文件为每个单元格添加图片,这会很好地工作。 当我尝试将 .png 放入辅助 sheet 和 .Duplicate 标志中时,无论我尝试什么,都会在 spritesheet 所在的 sheet 中创建标志。下面的剥离示例
我怎样才能复制不同的形状 Sheet?
Dim sh,shf as shape
dim is as range
Dim ws As Worksheet: Set ws = ActiveSheet
Set shf = Worksheets("flags").Shapes("flags")
ws.Activate
...
For each i in Selection.Cells
'get country code and calculate offsets
Set sh = shf.Duplicate 'this puts the shape in the flags sheet
....
正如其他 answers 发现的那样,您不能将一个形状从一个 sheet 移动到另一个,.Duplicate
只会在同一个地方复制。
最简单的步骤可能只是
Dim s1 As Worksheet, s2 As Worksheet
Dim shp2 As Shape
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("flags")
s2.Shapes(yourflag).copy
s1.Paste
set shp2 = s1.Shapes(s1.shapes.count)
如前所述,Duplicate
方法无法在不同的 sheet 中执行此操作。但是你可以通过下一种方式剪切和粘贴复制的形状(如果你喜欢复制而不使用经典的复制 - 粘贴解决方案):
Sub testShapeDuplicate()
Dim sh As Shape, shf As Shape, dLeft As Double, dTop As Double
Set shf = = Worksheets("flags").Shapes("flags")
Set sh = shf.Duplicate
sh.Name = "Dupl": dLeft = shf.left: dTop = shf.top
sh.Cut
With ActiveSheet.Next 'it will be moved in the next sheet. Use here what sheet you need, please
.Paste
With .Shapes(.Shapes.count)
.left = dLeft: .top = dTop 'put the shape in the same position
End With
End With
End Sub
除此之外,当您声明 Dim sh,shf as shape
时,只有 shf
对象被声明 As Shape
。 sh
声明为 As Variant
...