vba - Workbooks.Add(模板)失败,超出范围

时间:2014-07-26 16:34:33

标签: excel vba excel-vba

我正在尝试根据格式创建一个新的工作簿,就像现有的excel文件一样,我正在使用下面的代码

sub newWorkBook()    
Dim newWorkBook as New WorkBook
Set newWorkBook = WorkBooks.Add("Template.xls")
/*It is erroring on the above line*/
                newWorkBook.Application.DisplayAlerts = False
                With newWorkBook
                    .SaveAs "C:\new.xls"
                End With
                domainWorkBook.Save
                domainWorkBook.Application.DisplayAlerts = True
                domainWorkBook.Close
                Set newWorkBook = Nothing
End Sub

Set newWorkBook = WorkBooks.Add("Template.xls")上的错误表示下标超出了范围..

它还会打开一个名为Template1.xls的新Excel,并抛出错误......有什么建议吗?

1 个答案:

答案 0 :(得分:0)

为什么不打开模板 SaveAs 一个新名称?

Sub qwerty()
    Workbooks.Open "C:\TestFolder\Template.xls"
    ActiveWorkbook.SaveAs "C:\TestFolder\new.xls"
    ActiveWorkbook.Close
End Sub