SaveAs CSV on Mac

matt767

New Member
Joined
Apr 11, 2022
Messages
40
Office Version
  1. 365
Platform
  1. Windows
I am trying to save a new opened workbook as a csv on a Mac but keep running into error 1004: application or object defined error

here is the code:

VBA Code:
Dim directory As String
directory = "/Users/johnsmith/Desktop"
Dim mystring, clientname
mystring = Range("B2").Text
clientname = Mid(mystring, WorksheetFunction.Find("#", WorksheetFunction.Substitute(mystring, "/", "#", 3)) + 1, WorksheetFunction.Find("#", WorksheetFunction.Substitute(mystring, "/", "#", 4)) - WorksheetFunction.Find("#", WorksheetFunction.Substitute(mystring, "/", "#", 3)) - 1)
Dim wbname As String
wbname = WorksheetFunction.Substitute(Date, "/", ".") & " (" & clientname & ") " & "Upload"
Workbooks.Add
ChDir directory
ThisWorkbook.ActiveSheet.SaveAs Filename:=directory & "/" & wbname & ".csv", _
FileFormat:=xlCSVUTF8, CreateBackup:=False

I've tried Application.ThisWorkbook.SaveAs, ThisWorkbook.Sheets("Sheet1").SaveAs, ActiveWorkbook.ActiveSheet.SaveAs, ActiveWorkbook.Sheets("Sheet1").SaveAs, and ActiveWorkbook.SaveAs and all fail. office version is 365.

When I record a macro to do the same thing the code is like what I already tried:

VBA Code:
    Workbooks.Add
    ChDir "/Users/johnsmith/Desktop/"
    ActiveWorkbook.SaveAs FileName:="/Users/cameronspiller/Desktop/Book2.csv", _
        FileFormat:=xlCSVUTF8, CreateBackup:=False
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Have you tried removing the ActiveSheet from the code line?
Rich (BB code):
ThisWorkbook.ActiveSheet.SaveAs Filename:=directory & "/" & wbname & ".csv", _
FileFormat:=xlCSVUTF8, CreateBackup:=False
 
Upvote 0
Assuming it is the SaveAs line that is erring, if you add the line in red does what prints in the Immediate Window look correct for the filepath?
Also are you sure you want ThisWorkBook (that is the workbook the code resides in)?
I would think you need ActiveWorkbook

Rich (BB code):
ChDir directory
Debug.Print directory & "/" & wbname & ".csv"
ActiveWorkbook.SaveAs Filename:=directory & "/" & wbname & ".csv", _
FileFormat:=xlCSVUTF8, CreateBackup:=False
 
Upvote 0
ActiveWorkbook didn't work. This code runs fine on pc btw. Path looks fine. @MARK858
 
Last edited:
Upvote 0
What happens with
VBA Code:
    Dim directory As String
    directory = Application.PathSeparator & "Users" & Application.PathSeparator & "johnsmith" & Application.PathSeparator & "Desktop"
    Dim mystring, clientname
    mystring = Range("B2").Text
    clientname = Mid(mystring, WorksheetFunction.Find("#", WorksheetFunction.Substitute(mystring, "/", "#", 3)) + 1, WorksheetFunction.Find("#", WorksheetFunction.Substitute(mystring, "/", "#", 4)) - WorksheetFunction.Find("#", WorksheetFunction.Substitute(mystring, "/", "#", 3)) - 1)
    Dim wbname As String
    wbname = WorksheetFunction.Substitute(Date, "/", ".") & " (" & clientname & ") " & "Upload"
    Workbooks.Add
    ChDir directory
    ActiveWorkbook.SaveAs Filename:=directory & Application.PathSeparator & wbname & ".csv", _
                       FileFormat:=xlCSVUTF8, CreateBackup:=False
 
Upvote 0
Only thing that I can think of is if you are having the same issue as in the thread in the link below

 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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