[英]Run-time Error '9' - subscript out of range
When running my macro, I keep getting a run-time error stating the subscript is out of range. 运行宏时,我不断收到运行时错误,指出下标超出范围。 Anyone have any pointers?
有人有指针吗? The line that is not working is the Application.Workbooks(3).Activate
无效的行是Application.Workbooks(3).Activate
Below is the macro.. 下面是宏。
Sub Macro1()
'
' Macro1 Macro
'
'
ActiveSheet.Unprotect
ActiveSheet.Name = "M-YTD"
Range("E16:H16").Select
Selection.MergeCells = False
Columns("B:G").Select
Range("G11").Activate
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = "VarianceRpt"
Rows("1:10").Select
Range("A10").Activate
Selection.EntireRow.Hidden = True
Columns("G:G").ColumnWidth = 50
Range("G18").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "Variance Notes"
Range("F19").Select
Selection.Copy
Range("G18:G19").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Range("D16:F16").Select
Selection.MergeCells = True
ActiveSheet.Previous.Select
Range("E16:H16").Select
Selection.MergeCells = True
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Transfers"
Windows("Var Template.xls").Activate
Range("A1:M37").Select
Selection.Copy
Application.Workbooks(3).Activate
ActiveSheet.Paste
Sheets("VarianceRpt").Select
Sheets("VarianceRpt").Move Before:=Sheets(1)
End Sub
This is essentially a typo. 这本质上是一个错字。 a Subscript out of Range error always means that you're trying to index an array/collection with an invalid index parameter, in other words, the index you're asking for doesn't exist because it's outside the bounds of the sequence.
下标超出范围错误始终表示您正在尝试使用无效的索引参数为数组/集合建立索引,换句话说,您要的索引不存在,因为它不在序列的范围内。
Application.Workbooks(3)
means the third Workbook
object belonging to the current Application
instance. Application.Workbooks(3)
表示属于当前Application
实例的第三个 Workbook
对象。 If the current Application
instance contains 2 or fewer Workbooks
, then any index value greater than or equal to 3 will raise the error. 如果当前
Application
实例包含2个或更少的Workbooks
,则任何大于或等于3的索引值都将引发错误。 Likewise, an index of 0 will raise the same error. 同样,索引为0将引发相同的错误。
The same is true for any Collection
type, the index
parameter must be greater than 0, and less than or equal to the length of the collection. 对于任何
Collection
类型都适用, index
参数必须大于0,并且小于或等于集合的长度。 So: 所以:
ThisWorkbook.Worksheets(0)
will fail since 0
is not a valid index ThisWorkbook.Worksheets(0)
将失败,因为0
不是有效索引 ThisWorkbook.Worksheets(13)
will fail if there are 12 or fewer Worksheets
Worksheets
则ThisWorkbook.Worksheets(13)
将失败 ActiveSheet.ListObjects(1)
will fail if there aren't at least one ListObject
on the worksheet ListObject
,则ActiveSheet.ListObjects(1)
将失败 Arrays are usually base-zero, which means that their indices begin at 0, as a result their upper bound is 1 less than their length, so an array like: 数组通常为零基,这意味着它们的索引从0开始,因此其上限比其长度小1,因此数组如下:
Dim myArray
myArray = Array("A", "B", "C", "D")
Will have an upper bound of 3, and a lower bound of 0. Valid indices are therefore within the range of 0 to 3, so: 上限为3,下限为0。因此,有效索引在0到3的范围内,因此:
myArray(4)
will raise the error myArray(4)
将引发错误 myArray(0)
will return the value "A" myArray(0)
将返回值“ A” One sort-of exception is the Range.Cells
collection, which actually works differently: Range.Cells
集合是一种例外,它的工作原理实际上有所不同:
ActiveSheet.Range("A1").Cells(2)
refers to cell "A2" ActiveSheet.Range("A1").Cells(2)
引用单元格“ A2” 用人类语言,此错误将是“您要我(Excel或应用程序)转到第三个打开的工作簿并激活它。但是我的工作簿少于3个。因此,我向您显示此丑陋的错误。对不起:(“
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.