excel - 用单元格确定形状位置

标签 excel vba position cell shape

我有一个 Excel 日历,其中某些单元格上有一个形状。我希望能够查看哪些单元格具有形状,然后能够提取一些数据。
我搜索了一下,发现最好的选择是使用 TopLeftCell.Row 但我的代码似乎有错误。我复制了一个代码并尝试对其进行调整,这里是:

Sub ActiveShapeMacro()

Dim ActiveShape As Shape
Dim UserSelection As Variant

'Pull-in what is selected on screen
  Set UserSelection = ActiveWindow.Selection

'Determine if selection is a shape
  On Error GoTo NoShapeSelected
    Set ActiveShape = ActiveSheet.Shapes(UserSelection.Name)
  On Error Resume Next

'Do Something with your Shape variable
  Cells(Sheet1.Shapes(ActiveShape).TopLeftCell.Row, Sheet1.Shapes(ActiveShape).TopLeftCell.Column).Address
  MsgBox (ActiveShape.Address)

Exit Sub

'Error Handler
NoShapeSelected:
  MsgBox "You do not have a shape selected!"


End Sub

谢谢您的帮助! :)

最佳答案

错误在于:

Sheet1.Shapes(ActiveShape)

在哪里 Shapes在您提供 Object 时正在等待字符串(形状名称) (形状本身)

所以使用:
'Do Something with your Shape variable
  MsgBox Cells(ActiveShape.TopLeftCell.Row, ActiveShape.TopLeftCell.Column).Address

可以简化为:
   MsgBox ActiveShape.TopLeftCell.Address

此外变化:
On Error Resume Next 

至:
On Error GoTo 0

并继续观察那里发生的事情......

关于excel - 用单元格确定形状位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49325115/

相关文章:

Excel VBA - 使用 3 个行元素和 1 个列元素从数据透视表中获取数据

excel - 如何测试 Excel VBA 范围变量对整个列的引用?

vba - 如何从excel中的if语句中提取 'logical_test'?

vba - Excel VBA 数据透视表显示详细信息

python - 如何在不覆盖数据的情况下写入现有的 excel 文件(使用 pandas)?

c++ - 如何制作xls图表?

excel - 确定日期是否在开始/结束日期之间,以及自开始以来的平均天数

css - 位置固定的元素

css - Footable:更改手机屏幕中的行位置

javascript - 返回的 y 值大于实际 y 位置 [JS (no jQ)/DOM]