VBA Excel,形状数组。如何修复错误 "Index Into The Specified Collection Is Out Of Bounds"?
VBA Excel, shape array. How to fix the Error "Index Into The Specified Collection Is Out Of Bounds"?
在 Excel 中,我将三个形状分别命名为 ON_1
、ON_2
和 ON_3
。
我正在尝试构建一个 Shape 索引数组并获取 ShapeRange。
我有 VBA 代码,但我收到一条错误消息:
指定集合的索引超出范围。这是代码:
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
我希望获得变量 shprng
以包含数组中的所有形状名称。
但是我在这行代码中遇到错误:
Set shprng = sht.Shapes.Range(shape_index)
Run-time error 1004: The index into the specified collection is out of bounds
有什么想法吗?
您可以使用这些修复:
- 无需使用沉重的内存
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
希望对您有所帮助。
试试这个...
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
...是你的问题。它最初并未声明为数组。
现在是这个...
Dim shape_index() As Variant
在 Excel 中,我将三个形状分别命名为 ON_1
、ON_2
和 ON_3
。
我正在尝试构建一个 Shape 索引数组并获取 ShapeRange。
我有 VBA 代码,但我收到一条错误消息:
指定集合的索引超出范围。这是代码:
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
我希望获得变量 shprng
以包含数组中的所有形状名称。
但是我在这行代码中遇到错误:
Set shprng = sht.Shapes.Range(shape_index)
Run-time error 1004: The index into the specified collection is out of bounds
有什么想法吗?
您可以使用这些修复:
- 无需使用沉重的内存
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
希望对您有所帮助。
试试这个...
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
...是你的问题。它最初并未声明为数组。
现在是这个...
Dim shape_index() As Variant