'The ShapeRange Object must contain at least two items

When I run this code I get the following error: Run-time error: The ShapeRange Object must contain at least two items.

For i = 1 To counter

    x = Range("mainPositions").Item(i, 1)
    y = Range("mainPositions").Item(i, 2)
    nameX = Range("mainPositions").Item(i, 3)

    textX = Range("mainPositions").Item(i, 4)
        ActiveSheet.Shapes.AddShape(txtboxShape, x * hDistance + startX, y * vDistance + startY, txtboxWidth, txtboxHeight).Select

    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = textX
    Selection.ShapeRange.Name = nameX
    Call styleShape(CStr(nameX))
    If x * hDistance + startX + txtboxWidth > maxX Then
        maxX = x * hDistance + startX + txtboxWidth
    End If
    If y * vDistance + startY + txtboxHeight > maxY Then
        maxY = y * vDistance + startY + txtboxHeight
    End If
Next i


Solution 1:[1]

When you use AddShape it returns the added shape, so you can skip the Select and use the reference directly.
For example:

Dim shp
'...
'...
'get a reference to the added shape...
Set shp = ActiveSheet.Shapes.AddShape(txtboxShape, x * hDistance + startX, _
                         y * vDistance + startY, txtboxWidth, txtboxHeight)

'then use that reference
shp.TextFrame2.TextRange.Characters.Text = textX
shp.Name = nameX
styleShape CStr(nameX) 'call is deprecated

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Williams