VBA to save all worksheets as separate .xlsx files - macOS

ollyhughes1982

Well-known Member
Joined
Nov 27, 2018
Messages
793
Office Version
  1. 365
Platform
  1. MacOS
Hi,

I found the following code online (How to split a workbook to separate Excel files in Excel?). I hoped this would achieve my goal of saving all worksheets within a single .xlsx file and save (export / split) them to separate .xlsx files in the same file path location:

Sub Splitbook()
'Updateby20140612
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs FileName:=xPath & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

Is there something that needs changing for macOS? I have done a debug and it seems to be something with the 9th (file path) line (part in red text).

Screenshot 2023-05-22 at 18.21.47.png
Screenshot 2023-05-22 at 18.23.53.jpg


The other VBA code that I've used from the same website previously has always worked on my Mac no problem.

Thanks in advance,

Olly.
 
Try
VBA Code:
Application.ActiveWorkbook.SaveAs Filename:=xPath & Application.PathSeparator & xWs.Name, FileFormat:=62
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try
VBA Code:
Application.ActiveWorkbook.SaveAs Filename:=xPath & Application.PathSeparator & xWs.Name, FileFormat:=62
Thanks, but get an error with that line, unfortunately.
 

Attachments

  • Screenshot 2023-05-22 at 19.56.07.png
    Screenshot 2023-05-22 at 19.56.07.png
    181.4 KB · Views: 7
  • Screenshot 2023-05-22 at 19.56.29.png
    Screenshot 2023-05-22 at 19.56.29.png
    120.1 KB · Views: 8
Upvote 0
The current code:

Sub Splitbook()
'Updateby20140612
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ActiveWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs FileName:=xPath & Application.PathSeparator & xWs.Name, FileFormat:=xlCSVUTF8
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
In that case I don't know, it works fine on a PC.
 
Upvote 0
You're welcome.
Hopefully somebody with a Mac will be able to help.
 
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