[英]run time error “subscript out of range”
I have written the following script and I'm getting runtime error subscript out of range. 我编写了以下脚本,我的运行时错误下标超出了范围。 Can anyone help me with this?(The msgbox is used as a debugger)
任何人都可以帮我这个吗?(msgbox用作调试器)
Thanks in advance. 提前致谢。
Sub newbook()
Dim bk As Workbook, sht As Worksheet
MsgBox ("variables created")
Set bk = Workbooks.Add
MsgBox ("book is set")
With bk
.Title = "NewBook"
.SaveAs Filename:="NewBook.xls"
End With
MsgBox ("Book is created")
Set sht = bk.Sheets.Add
MsgBox ("sheet is set")
bk.Sheets(1).Name = "Hello 1"
bk.Sheets(2).Name = "Hello 2"
bk.Sheets(3).Name = "Hello 3"
bk.Sheets(4).Name = "Hello 4"
MsgBox ("everything is completed")
End Sub
Use 采用
bk.Sheets.Count
to get the number/last index of sheets, before you try to index into the collection. 在尝试索引到集合之前获取工作表的数量/最后一个索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.