简体   繁体   English

运行时错误'1004'对象'_Workbook'的方法'保存'失败

[英]Run-time error '1004' Method 'Save' of object '_Workbook' failed

I got this error while running an VBA application. 运行VBA应用程序时出现此错误。 I think this error is related to the following line in my code 我认为这个错误与我的代码中的以下行有关

ActiveWorkbook.Save

This is the whole code. 这是整个代码。

LDate = Date
LDate = Mid(LDate, 4, 2)
If LDate > 8 Then
Sheets("a").Cells(13, "H").Value = Sheets("a").Cells(13, "H").Value + 1000
Else
Sheets("a").Cells(13, "H").Value = Sheets("a").Cells(13, "H").Value + 1
End If
ActiveWorkbook.Save

Can someone explain the cause of this error and how I can tackle it. 有人可以解释这个错误的原因以及我如何解决它。

Please read below comments. 请阅读以下评论。

This is the subroutine that is getting executed when the first button is clicked. 这是单击第一个按钮时执行的子例程。

Sub import()
Dim Filt As String
Dim FilterIndex As Integer
Dim Title As String
Dim FileName As Variant
Dim finalrow As Integer
Dim alldata As String
Dim temp As String
Dim oFSO As New FileSystemObject
Dim oFS As TextStream

'Filt = "Cst Files (*.txt),*.txt"
'Title = "Select a cst File to Import"
'FileName = Application.GetOpenFilename(FileFilter:=Filt, Title:=Title)
'If FileName = False Then
'MsgBox "No File Was Selected"
'Exit Sub
'End If

'Call TestReference

' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
If diaFolder.SelectedItems.Count <> 0 Then
folderpath = diaFolder.SelectedItems(1)
folderpath = folderpath & "\"
'MsgBox diaFolder.SelectedItems(1)

Set diaFolder = Nothing

'RefreshSheet
On Error Resume Next
temp = folderpath & "*.txt"
sFile = Dir(temp)

Do Until sFile = ""
inputRow = Sheets("RawData").Range("A" & Rows.Count).End(xlUp).Row + 1
FileName = folderpath & sFile
Set oFS = oFSO.OpenTextFile(FileName)
Dim content As String
    content = oFS.ReadAll
content = Mid(content, 4, Len(content) - 3)
With Sheets("RawData").Range("A" & inputRow)
        .NumberFormat = "@"
        .Value = content
End With

oFS.Close
Set oFS = Nothing

alldata = ""
finalrow = Sheets("RawData").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("RawData").Activate
For i = inputRow To finalrow
alldata = alldata & Cells(i, "A").Value & " "
Cells(i, "A").Value = ""
Next i
Cells(inputRow, "B").Value = alldata

temp = StrReverse(FileName)
temp = Left(temp, InStr(1, temp, "\") - 1)
temp = StrReverse(temp)
temp = Left(temp, InStr(1, temp, ".") - 1)
Cells(inputRow, "A").Value = temp
Sheets("RawData").Cells(inputRow, "A").NumberFormat = "@"
sFile = Dir()
Loop
Else
MsgBox ("No Folder Selected")
End If
End Sub

How to make this code stop accessing the worksheet after executing this macro? 执行此宏后,如何使此代码停止访问工作表?

Although I think you should seriously consider refactoring your code , you should begin by referencing the correct workbook called by the .Save() Method. 虽然我认为您应该认真考虑重构代码 ,但应首先引用.Save()方法调用的正确工作簿。

    Workbooks("Insert_Workbook_Name_Here.xlsm").Save

Make sure that the workbook name and extension (.xlsm, .xls, .xlsx) match the file you are actually trying to save. 确保工作簿名称和扩展名(.xlsm,.xls,.xlsx)与您实际尝试保存的文件匹配。

This error happened in a macro that I wrote as well. 这个错误发生在我写的宏中。 I have this code to close a dialogue box. 我有这个代码来关闭一个对话框。

Private Sub CancelButton_Click()
   Unload Me
ThisWorkbook.Save
   End
End Sub

I received the same error because the workbook that was being loaded was from a "last saved" copy due to an update reboot that happened while the original was open. 我收到相同的错误,因为正在加载的工作簿来自“上次保存”的副本,原因是在原始文件打开时发生了更新重新启动。 Not sure how to avoid that in the future but thought it might be helpful to someone. 不知道将来如何避免这种情况,但认为它可能对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时错误“1004”:对象“_Workbook”的方法“SaveAs”失败 - Run-time error '1004': Method 'SaveAs' of object'_Workbook' failed 运行时错误&#39;1004&#39;:对象&#39;_Workbook的SaveAs方法失败 - Run-time error '1004': SaveAs Method of object '_Workbook failed 运行时错误“1004”-对象“工作簿”的方法“打开”失败 - Run-time Error '1004' - Method 'Open' of object 'Workbooks' failed 运行时错误&#39;1004&#39;:对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error '1004' : Method 'Range' of object'_Global' failed 运行时错误&#39;1004&#39;:对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error '1004': Method 'Range' of object '_Global' failed 运行时错误&#39;1004&#39;对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Run-time error '1004' Method 'Range' of object'_Global' failed 运行时错误&#39;1004&#39;:对象&#39;_Global&#39;的方法&#39;Range&#39;失败6 - Run-time error '1004' : Method 'Range' of object'_Global' failed 6 运行时错误“ 1004”:对象“工作簿”的方法“打开”失败 - Run-time error'1004': Method 'Open' of object 'Workbooks' failed 运行时错误'1004' - 对象'_Global'的方法'范围'失败 - Run-time error '1004' - Method 'Range' of object'_Global' failed 运行时错误“ 1004”:对象“ _Global”的方法“相交”失败 - Run-time error '1004': Method 'Intersect' of object' _Global' failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM