Run-time error "1004" Method "SaveAs"

rowbro

Board Regular
Joined
Dec 16, 2010
Messages
50
Good day, I have the below REALLY simple code to save a sheet in a workbook as CSV for upload to a database, however I keep getting the Run-time error "1004" Method "SaveAs" when I try and execute it. I can't figure out what I have got wrong - any advise?

Thanks

VBA Code:
Sub SaveCSV()

Application.DisplayAlerts = False

ThisWorkbook.Sheets("Export").Copy

x = Weekday(Date, vbSunday)
Select Case x
    Case 1
        x = 2
    Case 2
        x = 3
    Case Else
    x = 1
End Select

ActiveWorkbook.SaveAs FileName:="File saved on " & _
    Format(Date - x, "mm-dd-yyyy"), FileFormat:=xlCSV, CreateBackup:=True
    
ActiveWorkbook.Close

Application.DisplayAlerts = True

End Sub
 

Attachments

  • Screenshot 2020-07-01 11.49.35.png
    Screenshot 2020-07-01 11.49.35.png
    106.5 KB · Views: 43

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
1 Always declare variables
2 The default path is unpredictable - force the correct filepath within the code
3 Specify the sheet to be saved as illustrated

Rich (BB code):
Sub SaveCSV()
    Dim x As Long
    Const fpath = "C:\folder\subfolder\"
    Application.DisplayAlerts = False
    x = Weekday(Date, vbSunday)

    Select Case x
        Case 1
            x = 2
        Case 2
            x = 3
        Case Else
        x = 1
    End Select

    ActiveWorkbook.Sheets("Export").SaveAs Filename:=fpath & "File saved on " & _
        Format(Date - x, "mm-dd-yyyy"), FileFormat:=xlCSV, CreateBackup:=True
   
    ActiveWorkbook.Close

End Sub
 
Upvote 0
Thanks for the feedback. The problem is that I get the runtime error mentioned. (Run-time error '1004': Application-defined or object-defined error).

I made the changes you suggested, and still get the same. The issue is in the section:
VBA Code:
ActiveWorkbook.Sheets("Export").SaveAs Filename:=fpath & "File saved on " & _
        Format(Date - x, "mm-dd-yyyy"), FileFormat:=xlCSV, CreateBackup:=True

However, I can't seem to figure out what it is.
 
Upvote 0
The code works for me - I tested it before replying

1. Did you end this line with "\" ?
Rich (BB code):
Const fpath = "C:\folder\subfolder\"

2. Which version of Excel and windows is on your PC ?
 
Upvote 0
Sorry but I cannot help you sort out a mac specific problem - which is what this appears to be
It would be helpful for the future if you update your user profile to show your Office Version and Platform (Mac)

You could try recording a macro whilst saving the sheet as a csv and then tailor whatever code is provided
- it should provide everything

If that does not help, then I suggest that you start a new thread making it clear in the title you are looking for a Mac based solution "SaveAs CSV on Mac - Error 1004"

Good luck :)
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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