Home » excel » excel – Run-time error '1004' Method 'Save' of object '_Workbook' failed

excel – Run-time error '1004' Method 'Save' of object '_Workbook' failed

Posted by: admin May 14, 2020 Leave a comment

Questions:

I got this error while running an VBA application. 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?

How to&Answers:

Although I think you should seriously consider refactoring your code, you should begin by referencing the correct workbook called by the .Save() Method.

    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.

Answer:

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.