Search code examples
excelvbacollections

How to access the row property of a shape in a collection?


I have a collection of shapes in allShpsColl and I need to test if each shape resides in a group of rows called swimlanes here.

For i = 1 To swimlaneCount
    For j = 1 To allShpsColl.count
        If GetSwimlaneNum(allShpsColl.Item(j).Row) = i Then '<--Object doesn't support this property or method
            shpCount = shpCount + 1
            ReDim Preserve rowShpsArr(1 To shpCount)
            rowShpsArr(shpCount) = allShpsColl.Item(j)
        End If
    Next j
Next i

And the GetSwimlaneNum function is:

Function GetSwimlaneNum(ByRef lowRow As Integer) As Integer

Dim i As Integer

For i = 1 To 999
    If lowRow > (i - 1) * 9 And lowRow <= i * 9 Then
        GetSwimlaneNum = i
        Exit For
    End If

Next i End Function

How can I access the row property of the corresponding collection item so that I can compare values?


Solution

    • TopLeftCell represents the cell that lies under the upper-left corner of the shape object.
    If GetSwimlaneNum(allShpsColl.Item(j).TopLeftCell.Row) = i Then
    

    • If the shape may cover multiple lanes, you need two conditions to ensure that the shape is within a lane.
        If GetSwimlaneNum(allShpsColl.Item(j).TopLeftCell.Row) = i And _
           GetSwimlaneNum(allShpsColl.Item(j).BottomRightCell.Row) = i Then
    

    Microsoft documentation:

    Shape.BottomRightCell property (Excel)

    Shape.TopLeftCell property (Excel)