gpt4 book ai didi

vba - 如何使用VBA获取Excel中组内形状的BottomRightCell/TopLeftCell?

转载 作者:行者123 更新时间:2023-12-02 11:09:11 27 4
gpt4 key购买 nike

我已将一些形状分为一组。我们将其称为 Group1。我想要获取 Group1 中特定形状 Shape1 的 BottomRightCell/TopLeftCell。但每当我运行此代码时:

 ActiveSheet.Shapes("Group1").GroupItems("Shape1").BottomRightCell.Row

我得到了组右下角单元格的行,而不是特定 shape1 右下角单元格的行。我也尝试过这个:

ActiveSheet.Shapes("Shape1").BottomRightCell.Row

同样的事情也发生了。即使已分组,如何获取 Shape1 的右下角单元格?

最佳答案

似乎 GroupItems 中的项目 TopLeftCellBottomRightCell 存在错误并报告整个组。

相比之下,TopLeft 属性可以正确报告 GroupItems 集合中的项目。

作为解决方法,请考虑以下内容:

Sub Demo()
Dim ws As Worksheet
Dim grp As Shape
Dim shp As Shape, s As Shape
Set ws = ActiveSheet
Set grp = ws.Shapes("Group 1") '<~~ update to suit
With grp
For Each shp In .GroupItems
' Create a temporary duplicate shape
Set s = ws.Shapes.AddShape(msoShapeRectangle, shp.Left, shp.Top, shp.Width, shp.Height)

' Report the grouped shape to contrast the temporary shape result below
Debug.Print shp.TopLeftCell.Row, shp.BottomRightCell.Row
' Report the duplicate shape to see correct location
Debug.Print s.TopLeftCell.Row, s.BottomRightCell.Row

' Delete temporary shape
s.Delete
Next
End With
End Sub

在这里,我在组外的 GroupItems 集合中创建每个形状的副本,并报告其单元格位置。然后删除重复项。

我使用了矩形来演示,但其他形状类型应该类似

关于vba - 如何使用VBA获取Excel中组内形状的BottomRightCell/TopLeftCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43197444/

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