[英]Method 'SaveAs' of object'_Workbook' fails on other computers
I have a code that is splitting one workbook into just shy of 500. This code runs from one workbook and opens another to split.我有一个代码将一个工作簿拆分为将近 500 个。此代码从一个工作簿运行并打开另一个工作簿进行拆分。 On my computer this works every time with no fail.
在我的电脑上,这每次都可以正常工作。 On others the code will 1st stop at the ws.copy line.
在其他人身上,代码将首先停在 ws.copy 行。 They stop the code and start again.
他们停止代码并重新开始。 The code will then work for the first 180-220 sheets then pop the 'SaveAs" error. I thought, based on online reading, that this was a memory problem or a timing error. To fix this, I added a.wait function to no avail. Any help would be greatly appreciated. Code below for reference.
该代码将适用于前 180-220 张,然后弹出“另存为”错误。根据在线阅读,我认为这是内存问题或计时错误。为了解决这个问题,我添加了一个等待函数无济于事。任何帮助将不胜感激。下面的代码供参考。
Sub Splitbook()
Dim MyFile As String
MyFile = Sheets("Steps").Range("C6")
Windows(MyFile).Activate
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Sheets
ws.Copy
Name = ws.Range("C15").Value
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & Name & ".xlsx"
Application.ActiveWorkbook.Close False
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
ActiveWorkbook.Close SaveChanges:=False
MsgBox ("Split Complete. Press Ok to finish."), vbOKOnly, ("Thank you for your Patience.")
End Sub
EDIT编辑
Based on your feedback I have updated the code as follows:根据您的反馈,我更新了代码如下:
Sub Splitbook()
Dim MyFile As String
Dim wb As Workbook
MyFile = Sheets("Steps").Range("C6")
Set wb = Application.Workbooks(MyFile)
Windows(MyFile).Activate
Dim Loc As String
Loc = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In wb.Sheets
ws.Copy
Name = ws.Range("C15").Value
Application.ActiveWorkbook.SaveAs Filename:=Loc & "\" & Name & ".xlsx"
DoEvents
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
wb.Close SaveChanges:=False
MsgBox ("Split Complete. Press Ok to finish."), vbOKOnly, ("Thank you for your Patience.")
End Sub
Thus the dangers of ActiveWorkbook
, Activate
, Select
and ActiveSheet
.因此
ActiveWorkbook
、 Activate
、 Select
和ActiveSheet
的危险。 They may not be what you expect and it matters where the code is located.它们可能不是您所期望的,代码的位置很重要。 If you are running from a
Module
, Sheet
or ThisWorkbook
also makes a difference.如果您从
Module
运行, Sheet
或ThisWorkbook
也会有所不同。 If you are going to run from a module, you will need to qualify the locations - which Workbook
, Sheet
or Range
you are referencing.如果您要从模块运行,则需要限定位置 - 您正在引用的
Workbook
、 Sheet
或Range
。 When you create the new workbook by copying the sheet without a destination, that will be the active workbook for awhile, meanwhile you need to anchor to your starting file.当您通过复制没有目的地的工作表来创建新工作簿时,该工作簿将暂时成为活动工作簿,同时您需要锚定到您的起始文件。
Sub Splitbook()
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
Dim OrigWb As Workbook
Set OrigWb = ThisWorkbook 'or Set OrigWb = Workbooks("SplitFile") or some filename if not ThisWorkbook
Dim xPath As String
xPath = OrigWb.Path
For Each ws In OrigWb.Sheets
NewFileName = ws.Range("C15").Value
ws.Copy
With ActiveWorkbook
.SaveAs Filename:=xPath & "\" & NewFileName & ".xlsx"
.Close False
End With
MsgBox ("Split Complete. Press Ok to finish."), vbOKOnly, ("Thank you for your Patience.")
Next ws
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
This works and will work unless Range("C15")
is blank or invalid.除非
Range("C15")
为空白或无效,否则此方法有效且将有效。 The file will not save and you will not know because you turned off alerts.该文件不会保存,您也不会知道,因为您关闭了警报。 You may want to check that the range is not empty first.
您可能需要先检查范围是否为空。
If Not IsEmpty(ws.Range("C15").Value Then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.