如何确定线图对象的端点?

nsg*_*nsg 3 excel vba

我在 Excel 电子表格上有一个线条 (=autoshape) 绘图对象。我想确定它“指向”哪个单元格。为此,我需要知道起点和终点的坐标。

我可以使用.Top.Left.Width.Height以确定边界矩形,但线可以是在该矩形2个的不同位置。

Mic*_*riz 5

为此,您必须使用成员HorizontalFlipVerticalFlip。下面的函数应该做你想做的:

Function CellFromArrow(ByVal s As Shape) As Range
    Dim hFlip As Integer
    Dim vFlip As Integer

    hFlip = s.HorizontalFlip
    vFlip = s.VerticalFlip

    Select Case CStr(hFlip) & CStr(vFlip)
    Case "00"
        Set CellFromArrow = s.BottomRightCell
    Case "0-1"
        Set CellFromArrow = Cells(s.TopLeftCell.Row, s.BottomRightCell.Column)
    Case "-10"
        Set CellFromArrow = Cells(s.BottomRightCell.Row, s.TopLeftCell.Column)
    Case "-1-1"
        Set CellFromArrow = s.TopLeftCell
    End Select
End Function
Run Code Online (Sandbox Code Playgroud)

此代码在 Excel 2010 中进行了测试。似乎有效。希望这可以帮助!

编辑:如果您必须担心组中包含的形状,那么似乎唯一的解决方案是取消组合,遍历形状然后重新组合。类似于以下内容:

Dim s As Shape
For Each s In ActiveSheet.Shapes
    If s.Type = msoGroup Then
        Dim oldName as String
        Dim sGroup As GroupShapes
        Dim GroupMember as Shape
        Set sGroup = s.GroupItems
        oldName = s.Name 'To preserve the group Name
        s.Ungroup
        For Each GroupMember in sGroup
            'DO STUFF
        Next
        Set s = sGroup.Range(1).Regroup 'We only need to select one shape
        s.Name = oldName 'Rename it to what it used to be
    End If
Next
Run Code Online (Sandbox Code Playgroud)

您可以参考ShapeRange 文档以获取有关重新组合方法的更多信息。

让我知道这是否适合您!