gpt4 book ai didi

vba - 复制/粘贴具有形状的单元格

转载 作者:行者123 更新时间:2023-12-04 22:03:03 35 4
gpt4 key购买 nike

如何复制形状及其所在的单元格?当我手动复制时,形状会跟随单元格,但是当我使用宏进行复制时,我会得到除形状之外的所有其他内容。

Cells(sourceRow, sourceColumn).Copy
Cells(targetRow, targedColumn).PasteSpecial

...
Range("A1").copy
Range("B2").PasteSpecial Operation:=xlPasteAll

我已经尝试了所有我能想到的......但形状就是不会移动。

记录了手动复制/粘贴,这就是我得到的:
Range("A1").Select
Selection.Copy
Range("A3").Select
ActiveSheet.Paste

最佳答案

我会做这样的事情以避免必须实际选择单元格:

Sub MoveShape()

Dim s As Shape
Dim T, L, celWidth, shpWidth, celHeight, shpHeight As Double
Dim rng As Range
Dim ws as Worksheet

Set s = ws.Shapes(1).Duplicate ''You'll have to get the index of the shape you want to copy
Set rng = Range("A3") ''Set this to your target range

T = rng.Top
L = rng.Left

celWidth = rng.Width
shpWidth = s.Width

celHeight = rng.Height
shpHeight = s.Height

s.Top = T + (celHeight - shpHeight) / 2
s.Left = L + (celWidth - shpWidth) / 2

End Sub

这将复制您的形状,并将生成的克隆放在目标范围的中心。您可以通过修改 s.Left 来更改其在单元格中的位置。和 s.Top值(value)观。

您现在可以使用 Range("A3").Value = Range("A1").Value将单元格的实际值复制到目标范围

关于vba - 复制/粘贴具有形状的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30997121/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com