VBA Excel,形状数组。如何修复错误“指定集合的​​索引超出范围”?

时间:2019-04-11 09:41:30

标签: arrays excel vba range shapes

在Excel中,我将三个形状分别命名为+---+---+------+------+----+ |s |o |s_name|o_name|lang| +---+---+------+------+----+ |s2 |o2 |s2_fr |o2_fr |fr | |s2 |o1 |s2_fr |o1_fr |fr | |s1 |o1 |s1_fr |o1_fr |fr | |s1 |o1 |s1_en |o1_en |en | +---+---+------+------+----+ ON_1ON_2。 我正试图建立一个Shape索引数组并获取ShapeRange。 我有VBA代码,但出现错误提示: 指定集合的​​索引超出范围。这是代码:

ON_3

我希望获得变量Sub test() Dim sht As Worksheet Dim shprng As ShapeRange Dim shape_index As Variant Dim i As Long Set sht = ActiveSheet ReDim shape_index(1 To sht.Shapes.Count) For i = 1 To UBound(shape_index) shape_index(i) = i Next Set shprng = sht.Shapes.Range(shape_index) End Sub 以在数组中包含所有shapename。

但是相反,我在这行代码中遇到了错误:

shprng
  

运行时错误1004:指定集合中的索引超出范围

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您可以使用以下修复程序:

  • 无需使用大容量存储器variant数组。对于您的目标,一个简单的Integer数组就足够了。
  • 检查.Shapes.Count是否为0,否则您的代码将无法工作
  • (可选)可以使用UBound(shape_index) - LBound(shape_index) + 1获得实际的数组大小(即使在这种情况下也不需要,因为您已经知道下限了)

现在可以进行这些更正。这是代码:

Sub test()
    Dim sht As Worksheet
    Dim shprng As ShapeRange
    Dim shape_index() As Integer
    Dim i As Long

    Set sht = ActiveSheet

    'If no shape is present, exit sub
    If sht.Shapes.Count = 0 Then Exit Sub

    ReDim shape_index(1 To sht.Shapes.Count)
    For i = 1 To UBound(shape_index) - LBound(shape_index) + 1
        shape_index(i) = i
    Next

    Set shprng = sht.Shapes.Range(shape_index)

End Sub

希望有帮助。

答案 1 :(得分:0)

尝试一下...

$('#container').highcharts({

    chart: {
        type: 'gauge',
        borderWidth: 0,
    },

    title: {
        useHTML: true,
        verticalAlign: 'middle',
        floating: false,
        text: '<div style="text-align:center"><span class="gauge-count">80</span><span class="gauge-category-title">mg/L</span></div>'

    },

    pane: {
        startAngle: -160,
        endAngle: 160,
        background: null
    },

    // the value axis
    yAxis: {
        min: 0,
        max: 100,
        minorTickPosition: 'inside',
        minorTickColor: 'transparent',
        tickPosition: 'inside',
        tickColor: 'transparent',
        labels: {
            enabled: false
        },

        plotBands: [{
            from: 0,
            to: 30,
            className: 'red-band'
        }, {
            from: 30,
            to: 60,
            className: 'yellow-band'
        }, {
            from: 60,
            to: 100,
            className: 'green-band'
        }]
    },
    plotOptions: {
        gauge: {
            dataLabels: {
                formatter: function() {
                    return null;
                },
                y: 80,
                borderWidth: 0,
                useHTML: false
            },

        }
    },
    series: [{
        name: 'Speed',
        data: [80]
    }]

}, );

此行...

Sub test()
    Dim sht As Worksheet
    Dim shprng As ShapeRange
    Dim shape_index() As Variant
    Dim i As Long

    Set sht = ActiveSheet

    ReDim shape_index(1 To sht.Shapes.Count)
    For i = 1 To UBound(shape_index)
        shape_index(i) = i
    Next

    Set shprng = sht.Shapes.Range(shape_index)
End Sub

...是您的问题。它最初不是声明为数组。

现在是这个...

Dim shape_index As Variant