0

我编写了以下脚本,但运行时错误下标超出范围。谁能帮我解决这个问题?(msgbox 用作调试器)
提前致谢。

 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
4

1 回答 1

2

利用

bk.Sheets.Count

在尝试索引到集合之前获取工作表的数量/最后一个索引。

于 2013-07-16T10:52:06.750 回答