提问人:Masaaki Satoh 提问时间:11/16/2023 最后编辑:Masaaki Satoh 更新时间:11/16/2023 访问量:80
使用 AddShape 方法时,不会在参数指定的位置生成形状
Using the AddShape method, the shape is not generated at the position specified by the argument
问:
当我们使用 AddShape 方法生成形状时,我们遇到了麻烦,因为位置可能会移动。此外,结果似乎因所使用的设备而异。
以下是测试代码及其结果。
我们如何在指定为参数的位置稳定地生成形状?
测试代码 (Excel VBA)
Public Sub CreareShapes()
' Variables
Dim c As Range: Set c = Sheet1.Range("A1")
Dim d As Range: Set d = Sheet2.Range("A1")
Dim s As Shape
Dim i As Long
Dim coefficient As Long: coefficient = 29
Dim leftPosition As Double
' Create title for log
Sheet2.Cells.Clear
d.Value = "IN"
d.Offset(ColumnOffset:=1).Value = "OUT"
d.Offset(ColumnOffset:=2).Value = "DIFF"
Set d = d.Offset(1)
' Clear shapes
For Each s In Sheet1.Shapes
s.Delete
Next
' Create shapes
For i = 1 To 100
' Create shape
leftPosition = c.Left + (c.Left + 1) / coefficient
Set s = Sheet1.Shapes.AddShape(msoShapePentagon, leftPosition, c.Top, c.Width * 3, 10)
' Write logs
d.Value = leftPosition '= IN
d.Offset(ColumnOffset:=1).Value = s.Left '= OUT
d.Offset(ColumnOffset:=2).Formula = "=A" & d.Row() & "-B" & d.Row() '= DIFF
' Move cursors
Set c = c.Offset(1, 1)
Set d = d.Offset(1)
Next
End Sub
Ref. Shapes.AddShape 方法 (Excel)
https://learn.microsoft.com/en-us/office/vba/api/excel.shapes.addshape
参考图片
答: 暂无答案
评论
msoShapePentagon
position shifts left or right