如何确定线条图对象的终点?

时间:2015-07-24 15:26:28

标签: excel vba excel-vba

我在Excel电子表格上有一个(=自动形状)绘图对象。我想确定它指向哪个单元格#34;至。为此,我需要知道起点和终点的坐标。

我可以使用.Top.Left.Width.Height来确定边界矩形,但该行可能位于该矩形的2个不同位置。

1 个答案:

答案 0 :(得分:4)

要执行此操作,您必须使用成员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

此代码在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

有关Regroup方法的详细信息,请参阅ShapeRange Documentation

请告诉我这是否适合您!