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

ollyhughes1982

Well-known Member
Joined
Nov 27, 2018
Messages
677
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.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try using
VBA Code:
Application.ActiveWorkbook.SaveAs FileName:=xPath & Application.PathSeparator & xWs.Name & ".xlsx"
 
Upvote 0
Try using
VBA Code:
Application.ActiveWorkbook.SaveAs FileName:=xPath & Application.PathSeparator & xWs.Name & ".xlsx"
I tried that, but it unfortunately didn't work. It just gives me one new blank workbook called 'Sheet1'. No errors though.

New code as below:

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 & Application.PathSeparator & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Ok, change this line
VBA Code:
For Each xWs In ThisWorkbook.Sheets
from ThisWorkbook to ActiveWorkbook
 
Upvote 1
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

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