各位神手午安,最近在寫將活頁簿內不同工作表另存成不同檔案的小程式。
但三不五時會跑出 Class Worksheet的Copy方法失敗的error '1004'。
奇怪的是偶爾會偶爾不會,或是跑幾個迴圈後就error,請問各位可以修改的方向或範例,
感激不盡,版本是Office 2010,程式如下:
Sub splitbook()
Dim xPath as string
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlarts = False
For Each xWs in ActiveWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name, _
FileFormat:=xlOpenXMLWorkbook
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlarts = True
Application.ScreenUpdating = True
End Sub