excel - VBA Shape.Placement 属性仍然允许调整大小
问题描述
我正在使用下面的代码从工作表“模板复制第一行并粘贴到所有其他工作表的第一行。第一行包含需要保持相同大小的形状。
运行宏时,“模板”中的形状会在所有其他工作表上调整大小。因此我用这条线设置了placement 属性Shp.Placement = xlFreeFloating
。
但是,它们在粘贴过程中会继续调整大小。然而,在它们被粘贴后,如果单元格大小发生变化,它们将不再调整大小。
第一次粘贴时如何防止形状调整大小?
Dim ws As Worksheet
Dim HeaderRow As Range
Dim Shp As Shape
Set HeaderRow = Worksheets("Template").Range("1:1")
For Each ws In ActiveWorkbook.Worksheets
For Each Shp In ws.Shapes
Shp.Placement = xlFreeFloating
Next Shp
If ws.Name <> "Template" Then
For Each Shp In ws.Shapes
If Shp.TopLeftCell.Row = 1 Then
Shp.Delete 'Delete shapes before pasting or they multiply
End If
Next Shp
HeaderRow.Copy
ws.Range(HeaderRow.Address).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ws.Paste
End If
Next ws
解决方案
请尝试下一个代码:
Sub copyShapesMove()
Dim ws As Worksheet, wsT As Worksheet, HeaderRow As Range, Shp As Shape
Set wsT = Worksheets("Template")
Set HeaderRow = wsT.Range("1:1")
'set the header shapes placement property BEFORE COPYING:
For Each Shp In wsT.Shapes
If Not Intersect(Shp.TopLeftCell, HeaderRow) Is Nothing Then
Shp.placement = xlMove
End If
Next
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Template" Then
For Each Shp In ws.Shapes
If Shp.TopLeftCell.row = 1 Then
Shp.Delete 'Delete shapes before pasting or they multiply
End If
Next Shp
HeaderRow.Copy
ws.Range(HeaderRow.Address).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ws.Paste
End If
Next ws
End Sub
编辑:
上面的代码仅适用于比 Excel 2007 更新的版本,它看起来有一个错误并且不关心(在 VBA 中)“移动但不使用单元格调整大小”属性。因此,以下代码将要复制的形状尺寸放在一个数组中,并在复制后调整复制形状的大小:
Sub copyShapesSameDimensions()
Dim ws As Worksheet, wsT As Worksheet, HeaderRow As Range, Shp As Shape
Dim arrSh, k As Long, El, arrProp
Set wsT = Worksheets("Template")
Set HeaderRow = wsT.Range("1:1")
'Put the shapes to be copied dimensions in an array:
ReDim arrSh(wsT.Shapes.Count - 1)
For Each Shp In wsT.Shapes
If Shp.TopLeftCell.Row = 1 Then
arrSh(k) = Shp.Name & "|" & Shp.Width & "|" & Shp.Height & "|" & Shp.Left: k = k + 1
End If
Next
ReDim Preserve arrSh(k - 1)
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Template" Then
For Each Shp In ws.Shapes
If Shp.TopLeftCell.Row = 1 Then
Shp.Delete
End If
Next Shp
HeaderRow.Copy
ws.Range(HeaderRow.Address).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ws.Paste
'put the dimensions back on each shape:
For Each El In arrSh
arrProp = Split(El, "|")
For Each Shp In ws.Shapes
If Shp.TopLeftCell.Row = 1 Then
If Shp.Name = arrProp(0) Then
Shp.Width = CDbl(arrProp(1))
Shp.Height = CDbl(arrProp(2))
Shp.Left = CDbl(arrProp(3))
Exit For
End If
End If
Next
Next El
End If
Next ws
End Sub
推荐阅读
- monitoring - 当 GKE 工作负载出现问题时,在 Stackdriver 中创建事件和通知
- javascript - 三元运算符确定对象值
- mysql - 如何通过计算减少多行的值
- c - Microchip PIC12F1501 写/读闪存
- php - 如何在 HTML/PHP 中比较两个提交的数字,使得第二个大于第一个?
- java - 具有自定义查询的 SequenceGenerator 到另一个数据库
- scala - Slick Group 通过聚合多个列
- rest - 使用带有代理和证书的 Apache Airflow SimpleHTTPOperator
- java - Scala 编译时错误:没有为参数evidence$2 找到隐含:BodyWritable[Map[String, Object]]
- azure - 在天蓝色的事件总线上,一封信进入死信队列需要多少时间