用单元格确定形状位置

Determine shape position with cell

我有一个 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

并继续关注那里发生的事情...

这里有一个简单的方法来确定是否选择了一个范围或形状,以及它是否是一个形状,它在哪里:

Sub skjdkffdg()
    Dim s As Shape, typ As String

    typ = TypeName(Selection)

    If typ = "Range" Then
        MsgBox " you have a range selected: " & Selection.Address
    Else
        Set s = ActiveSheet.Shapes(Selection.Name)
        MsgBox "you have a Shape selected: " & s.TopLeftCell.Address
    End If
End Sub

这假设工作表上只有形状和范围。