Method 'SaveAs" of object'_Workbook' failed - error 1004

snuffnchess

Board Regular
Joined
May 15, 2015
Messages
71
Office Version
  1. 365
Platform
  1. Windows
I am getting an error when saving a workbook SOMETIMES.... and it is completely baffling me. Cannot figure out a rhyme or reason to when it works.

I have verified using a msgbox that the saveas folder / file name is correct. The filtered data copies to a newbook... I see it happen - the message box comes up showing that it is correct.. I hit next on the messagebox and then I SOMETIMES get a "Run Time Error '1004' method 'SaveAs' of object'_Workbook' failed"

If I click the debug button, and then then F8 to continue on, I get a "Document not saved" error box. But here is the kicker. When I go to the directory that it is supposed to save the file in.. the file is there!

Im so confused... Help!!!

VBA Code:
Option Explicit
Public sfile As String
Public adfile As String
Public sFolder As String
Public sFilename As String
Public adfolder As String
Public sdir As String
Public sfolca As String


Sub cceod()

Dim ob As Workbook
Dim eoddata As Worksheet


Dim cwdown As Workbook

Dim nb As Workbook
Dim ns As Worksheet


Dim REC1 As Variant
Dim trandate As String
Dim sdata As Long


Dim eoddatasortArr
Dim eoddatacel As Range
Dim r As Range
Dim rv As Range


Dim fldr As FileDialog
Dim strpath As String
Dim sItem As String

Dim eodlrow As Long
Dim eodlcol As Long


Dim i As Integer


Application.ScreenUpdating = False
Application.DisplayAlerts = False


Set ob = ThisWorkbook
Set eoddata = ob.Sheets("EOD")



eoddata.Visible = xlSheetVisible
eoddata.Cells.Clear
    trandate = InputBox("Enter Transaction Date mm/dd/yyyy format")
        
    

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr
            .Title = "Select a Folder to save Files to"
            .AllowMultiSelect = False
            .InitialFileName = strpath
            If .Show <> -1 Then Exit Sub
            sItem = .SelectedItems(1)
        End With
        
    sfolca = sItem & "\" & Format(trandate, "yyyy-mm-dd") & "\"
    
    If Len(Dir(sfolca, vbDirectory)) = 0 Then
        MkDir sfolca
    End If
    If Len(Dir(sfolca & "Batch Success", vbDirectory)) = 0 Then
        MkDir sfolca & "Batch Success"
    End If
    If Len(Dir(sfolca & "Batch Fail", vbDirectory)) = 0 Then
        MkDir sfolca & "Batch Fail"
    End If
    If Len(Dir(sfolca & "Manual Success", vbDirectory)) = 0 Then
        MkDir sfolca & "Manual Success"
    End If
    If Len(Dir(sfolca & "Manual Fail", vbDirectory)) = 0 Then
        MkDir sfolca & "Manual Fail"
    End If
        
    REC1 = Application.GetOpenFilename(Title:="Select Data", FileFilter:="Excel Files (*.xls*; *.csv),*.xls*; *.csv")
        If REC1 <> False Then
            Set cwdown = Application.Workbooks.Open(REC1)
                With cwdown.Sheets(1).UsedRange
                    .Resize(.Rows.Count, .Columns.Count).Offset(0, 0).Copy
                End With
            eoddata.Range("A1").PasteSpecial xlPasteValues
                Columns.AutoFit
            
            cwdown.Close False
        End If

    
    eodlrow = eoddata.Range("A1").End(xlDown).Row
    
        
    eoddata.Columns("L:N").Select
        With Selection
            Selection.NumberFormat = "General"
            .Value = .Value
            .NumberFormat = "0.00"
        End With
        
    
 


    eoddata.Cells(1, 1).Value = "POSTDATE"
    eoddata.Cells(1, 2).Value = "CUSTOMERNUMBER"
    eoddata.Cells(1, 3).Value = "AMOUNT"
    eoddata.Cells(1, 4).Value = "TYPE"
    eoddata.Cells(1, 5).Value = "CHECK/CC NUMBER"
    eoddata.Cells(1, 6).Value = "INVOICENUMBER"
    eoddata.Cells(1, 7).Value = "OPTIONAL DESCRIPTION FIELD"
    
    eodlcol = Range("A1").End(xlToRight).Column
    eoddata.Cells(1, eodlcol + 1).Value = trandate
        eoddata.Cells(1, eodlcol + 1).Select
            Selection.NumberFormat = "yyyy-mm-dd"
            sdata = eoddata.Cells(1, eodlcol + 1)
    
        
    Set r = eoddata.Range(Cells(1, 1), Cells(eodlrow, eodlcol))
    
   
    r.AutoFilter field:=14, Criteria1:="Batch Upload"
    r.AutoFilter field:=12, Criteria1:="success"
    
    Set rv = eoddata.Range(Cells(1, 1), Cells(eodlrow, 7)).SpecialCells(xlCellTypeVisible)
    
        If rv.Rows.Count > 1 Or rv.Areas.Count > 1 Then
            rv.Copy
            
            Set nb = Workbooks.Add
            Set ns = nb.Worksheets(1)
            ns.Cells(1, 1).PasteSpecial xlPasteAll
            'ns.Columns.AutoFit
            Application.CutCopyMode = False
            nb.Activate
            
                sFilename = "batchsuccess"
                MsgBox sfolca & "Batch Success\" & sFilename & ".xlsx"
                nb.SaveAs sfolca & "Batch Success\" & sFilename & ".xlsx", 51
                
            nb.Close False
        End If
        
    
    r.AutoFilter field:=14, Criteria1:="Batch Upload"
    r.AutoFilter field:=12, Criteria1:="<>success"
    
    Set rv = eoddata.Range(Cells(1, 1), Cells(eodlrow, 7)).SpecialCells(xlCellTypeVisible)
    
        If rv.Rows.Count > 1 Or rv.Areas.Count > 1 Then
            rv.Copy
            
            Set nb = Workbooks.Add
            Set ns = nb.Worksheets(1)
            ns.Cells(1, 1).PasteSpecial xlPasteAll
            Application.CutCopyMode = False
            nb.Activate
            
                sFilename = "batchfail"
                MsgBox sfolca & "Batch Fail\" & sFilename & ".xlsx"
                nb.SaveAs sfolca & "Batch Fail\" & sFilename & ".xlsx", 51
            nb.Close False
        End If
        
    r.AutoFilter field:=14, Criteria1:="<>Batch Upload"
    r.AutoFilter field:=12, Criteria1:="success"
    
    Set rv = eoddata.Range(Cells(1, 1), Cells(eodlrow, 7)).SpecialCells(xlCellTypeVisible)
    
        If rv.Rows.Count > 1 Or rv.Areas.Count > 1 Then
            rv.Copy
            
            Set nb = Workbooks.Add
            Set ns = nb.Worksheets(1)
            ns.Cells(1, 1).PasteSpecial xlPasteAll
            Application.CutCopyMode = False
            nb.Activate
            
                sFilename = "manualsuccess"
                MsgBox sfolca & "Manual Success\" & sFilename & ".xlsx"
                nb.SaveAs sfolca & "Manual Success\" & sFilename & ".xlsx", 51
            nb.Close False
        End If
        
        
    r.AutoFilter field:=14, Criteria1:="<>Batch Upload"
    r.AutoFilter field:=12, Criteria1:="<>success"
    
    Set rv = eoddata.Range(Cells(1, 1), Cells(eodlrow, 7)).SpecialCells(xlCellTypeVisible)
    
        If rv.Rows.Count > 1 Or rv.Areas.Count > 1 Then
            rv.Copy
            
            Set nb = Workbooks.Add
            Set ns = nb.Worksheets(1)
            ns.Cells(1, 1).PasteSpecial xlPasteAll
            Application.CutCopyMode = False
            nb.Activate
            
                sFilename = "manualfail"
                MsgBox sfolca & "Manual Fail\" & sFilename & ".xlsx"
                nb.SaveAs sfolca & "Manual Fail\" & sFilename & ".xlsx", 51
            nb.Close False
        End If
    
    eoddata.ShowAllData
    
    
    
    
    


'eod.Visible = xlsheethidden
Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Method 'SaveAs" of object'_Workbook' failed - error 1004
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Update to this issue.... I have narrowed down that the errors seem to only be happening when the files are being saved to a directory that is backed up by OneDrive / Sharepoint. If select a local directory, the code works without any issue. Has anybody heard of any issue that would cause this? Any Workarounds?
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Method 'SaveAs" of object'_Workbook' failed - error 1004
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
I am sorry about that... i was not aware of this, and will make sure it does not happen again.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top