尝试对工作表上的形状进行分组时出现“找不到指定名称的项目”错误

时间:2018-11-20 16:47:01

标签: excel vba shapes

我正在编写一个VBA程序,以选择性地对一些圆角矩形形状进行分组。这些组将有一组,所以我想将它们存储在数组中。 (例如,我想让dataSeriesGroup(1)拥有一组说三个圆角矩形,dataSeriesGroup(2)拥有一组其他三个舍入矩形,依此类推)。我正在尝试使用.Name属性将它们分配给组,如下所示:

Dim ctr, ctr2, seriesCount, dataCount as Integer
Dim dataSeriesGroup() as Shape
Dim dataPoint() as Shape
Dim dTop, dLeft, dWidth, dHeight as long
Dim dataPointName as Variant

<Bunch of code to calculate values of dTop, dLeft, dWidth, dHeight, seriesCount, dataCount>

Redim dataSeriesGroup(seriesCount)
Redim dataPoint(dataCount, dataSeriesCount)
Redim dataPointName(dataCount)

For ctr = 1 to seriesCount

 For ctr2 = 1 to dataCount
   Set dataPoint(ctr2, ctr) = ActiveSheet.Shapes.AddShape(msoShapeRoundedRectangle, dLeft, dTop, dWidth, dHeight)
   dataPointName(ctr2) = dataPoint(ctr2, ctr).Name
 Next ctr2

Set dataSeriesGroup(ctr) = Activesheet.Shapes(Array(dataPointName)).Group

Next ctr

一切正常,但是当我尝试设置dataSeriesGroup(ctr)时,出现错误“运行时错误'-2147352571(80020005)':找不到具有指定名称的项目”。

有人可以提供一些有关我做错事情的指导吗?

1 个答案:

答案 0 :(得分:0)

问题出在声明变量datapointName的方式上。您想要构建一个行为与Array函数返回的数组相同的数组,该函数返回一个从零开始的变量数组

Dim datapointName() As Variant '<== Notice the parentheses.

注意ReDims,因为通常您不希望在数组的远端悬挂空值,所以:

ReDim dataPointName(0 To dataCount - 1) '<== That's dataCount elements!

另请参见下面的示例代码中有关变量声明的注释。

最后,由于dataPointName已经是,使用Range集合的Shapes属性获取一个子集,并删除对Array()的调用:

Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group

将所有内容放在一起,下面是一些基于您的功能演示代码:

Sub DoTheShapesThing()
    'Note: in VBA, to each variable its type; otherwise: Variant.
    'I've renamed some variables for clarity.
    Dim seriesIndex As Integer
    Dim dataIndex As Integer
    Dim seriesCount As Integer
    Dim dataCount As Integer
    Dim dataSeriesGroup() As Shape
    Dim dataPoint() As Shape
    'Haven't altered your position and size variables, but the type should typically be Double.
    Dim dTop As Long
    Dim dLeft As Long
    Dim dWidth As Long
    Dim dHeight As Long
    Dim dataPointName() As Variant '<== Here, the parentheses make all the difference! You want an array of Variants, just like the Array function returns.

    'I've added this declaration for the code to compile. REMOVE IT! You've probably declared this variable elsewhere.
    Dim dataseriesCount As Long

    'Test values...
    seriesCount = 2
    dataCount = 2
    dataseriesCount = seriesCount '<== Note that dataseriesCount must be >= seriesCount so the code below doesn't go "Subscript out of range".
    dLeft = 100: dTop = 100: dWidth = 100: dHeight = 100

    ReDim dataSeriesGroup(0 To seriesCount - 1)
    ReDim dataPoint(0 To dataCount - 1, 0 To dataseriesCount - 1)
    ReDim dataPointName(0 To dataCount - 1)

    For seriesIndex = 0 To seriesCount - 1
        For dataIndex = 0 To dataCount - 1
            'Took some liberties with shape disposition here...
            Set dataPoint(dataIndex, seriesIndex) = ActiveSheet.Shapes.AddShape( _
                msoShapeRoundedRectangle, _
                dLeft + 10 * (seriesIndex + dataIndex), _
                dTop + 10 * (seriesIndex + dataIndex), _
                dWidth, _
                dHeight)

            dataPointName(dataIndex) = dataPoint(dataIndex, seriesIndex).Name
        Next dataIndex

        Set dataSeriesGroup(seriesIndex) = ActiveSheet.Shapes.Range(dataPointName).Group
    Next seriesIndex
End Sub