我在添加带有名称的多个工作表时遇到问题。创建文件后,将其保存并开始添加工作表即可,但在尝试添加第3个工作表(名为“DPG”)时崩溃(发生自动化错误/异常 - 需要重启Excel等)。以下是有问题的代码部分:
'CREATE DATA OUTPUT FILE
Set NewBook = Workbooks.Add
With NewBook
.Title = "DATA OUTPUT"
.Subject = "OUTPUT"
.SaveAs Filename:="H:\REACKS\DATA OUTPUT.xls"
End With
Worksheets.Add().Name = "ALL"
Worksheets.Add().Name = "CORP"
Worksheets.Add().Name = "DPG"
Worksheets.Add().Name = "ESG"
Worksheets.Add().Name = "FSG"
Worksheets.Add().Name = "KEN"
非常感谢任何帮助!
答案 0 :(得分:0)
在第一个NewBook.Activate
之前添加此行Worksheets.Add()...
修改: Nvm,这不起作用。相反,试试这个:
Dim NewBook As Workbook
Set NewBook = Workbooks.Add
With NewBook
.Title = "DATA OUTPUT"
.Subject = "OUTPUT"
.SaveAs Filename:="C:\Documents and Settings\rfavron\Bureau\Bernard\DATA OUTPUT.xls"
.Worksheets.Add().Name = "ALL"
.Worksheets.Add().Name = "CORP"
.Worksheets.Add().Name = "DPG"
.Worksheets.Add().Name = "ESG"
.Worksheets.Add().Name = "FSG"
.Worksheets.Add().Name = "KEN"
End With
答案 1 :(得分:0)
我解决了这个问题!它没有正确保存为.xls,即使它已在文件名中指定。我添加了一个文件格式定义设置为.xls。
.SaveAs Filename:="H:\REACKS\DATA OUTPUT.xls", FileFormat:=xlExcel8
这似乎可以解决问题!