Code works on Windows but not Mac

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have the below piece of code which runs fine on a Windows PC but I need it to run on a mac and when I run it it keeps erroring at the save file stage. The issue seems to be with the directory, this is the one I use: /Users/stroffso/desktop

Any help would be greatly appreciated

VBA Code:
Sub ExportNatalieAndPoppy()
    Dim wb As Workbook
    Dim wsNatalie As Worksheet
    Dim wsControls As Worksheet
    Dim saveDirectory As String
    Dim natalieFileName As String
    Dim selectedName As String
    Dim namesArray As Variant
    Dim i As Integer
    
    Application.DisplayAlerts = False
    
    ' Set references to workbook and worksheets
    Set wb = ThisWorkbook
    Set wsNatalie = wb.Sheets("Summary")
    Set wsControls = wb.Sheets("Controls")
    
    ' Get the save directory from Cell D18 on the Controls tab
    saveDirectory = wsControls.Range("D18").Value
    
    ' Define an array of names
    namesArray = Array("Natalie James", "Frank Lee")
    
    ' Loop through the names
    For i = LBound(namesArray) To UBound(namesArray)
        selectedName = namesArray(i)
        
        ' Set the output Excel filename
        natalieFileName = selectedName & ".xlsx"
        
        ' Update the name in Cell C1
        wsNatalie.Range("C1").Value = selectedName
        
        ' Copy the "Summary" tab to a new workbook
        wsNatalie.Copy
        ActiveWorkbook.SaveAs fileName:=saveDirectory & "\" & natalieFileName, FileFormat:=xlOpenXMLWorkbook
        ActiveWorkbook.Close SaveChanges:=False
    Next i
    
    ' Clean up
    Set wsNatalie = Nothing
    Set wb = Nothing
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Maybe if it is breaking on the SaveAs line
Rich (BB code):
ActiveWorkbook.SaveAs Filename:=saveDirectory & Application.PathSeparator & natalieFileName, FileFormat:=xlOpenXMLWorkbook
 
Upvote 0
What's the error?
unnamed.png
 
Upvote 0
We don't have visibility of what is in wsControls.Range("D18") ie "saveDirectory", so how about:

VBA Code:
    Dim FullFileName As String
    FullFileName = Replace(saveDirectory & "\" & natalieFileName, "\", Application.PathSeparator)
    ActiveWorkbook.SaveAs Filename:=FullFileName, FileFormat:=xlOpenXMLWorkbook
 
Upvote 0
We don't have visibility of what is in wsControls.Range("D18") ie "saveDirectory", so how about:

VBA Code:
    Dim FullFileName As String
    FullFileName = Replace(saveDirectory & "\" & natalieFileName, "\", Application.PathSeparator)
    ActiveWorkbook.SaveAs Filename:=FullFileName, FileFormat:=xlOpenXMLWorkbook
Thanks all for your comments, the string in Cell D18 is /Users/stroffso/desktop
 
Upvote 0
Did you try @MARK858's line ?
If it didn't work then before that line add this line:
VBA Code:
Debug.Print saveDirectory & Application.PathSeparator & natalieFileName

Then have a look in the immediate window (Ctrl+G if its not visible) and see if the file path and name make sense.
If it visually make sense but its not working, copy the line from the immediate window and try it by pasting it manually in a save as dialogue box and see what you need to change to make it save.
 
Upvote 0
I have gone with an alternative approach which was removing the save to specific directory as the above didnt work, the export still works though so a little bit of manual intervention is ok, thanks to all for your help
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
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