如果之前已经回答过,或者解决方案非常简单,请道歉。我得到了
运行时错误' 1004',无法访问'表'。
我试图将工作簿拆分为单独的.txt文件。
Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.SaveAs _
FileName:=MyPath & "\" & sht.Name, FileFormat:=xlTextMac,
CreateBackup:=False
ActiveWorkbook.Close savechanges:=False
Next sht
End Sub
答案 0 :(得分:0)
看看这是否有帮助 - 我创建了显式引用,以尽量减少Active
工作表/工作簿的使用:
Sub Splitbook()
Dim tempWS As Worksheet
Dim tempWB As Workbook
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
Set tempWB = Workbooks.Add
Set tempWS = tempWB.Sheets(1)
sht.Cells.Copy
tempWS.Cells.PasteSpecial Paste:=xlPasteValues
tempWS.Cells.PasteSpecial Paste:=xlPasteFormats
tempWB.SaveAs Filename:=MyPath & "\" & sht.Name, FileFormat:=xlTextMac, CreateBackup:=False
tempWB.Close savechanges:=False
Next sht
End Sub