Save only a specific worksheet in a workbook with Excel

The issue here is I wanted to export/save only a specific sheet from a workbook. Which isn’t possible currently with scripts. Every command that I tried leads to export/save the whole workbook.

tell application "Microsoft Excel"
      activate
      set display alerts to false
      set workbookName to "/Users/macOS/Documents/"
      tell active sheet to save in workbookName as "CSV file format" --tried with different formats as well & tried `sheet 1 of active workbook to save`
end tell

Is that something you can do from the user interface?

Just a thought: save the original document, then delete the unwanted sheets, then Save As to a new file?

try to set visible of all other sheets to sheet hidden

tell application "Microsoft Excel"
		save workbook
		save workbook as active workbook filename "path/to/file/" file format text Mac file format
		--close active workbook with saving --in case you don't need it open anymore
		set display alerts to false
	end tell

Yes, you can by right-clicking on the sheet’s tab and selecting Move or Copy... and you can set the destination as a new workbook and specify whether to move or copy the sheet.

To do this in a script, you can use the copy worksheet command to create a new workbook from a worksheet and then you can save that new workbook. Leaving off the after and before parameters causes Excel to create a new workbook containing the sheet.

tell application "Microsoft Excel"
	activate
	tell active workbook
		copy worksheet worksheet "Sheet2"
		-- the active workbook changes to the new one
		-- but in my testing it doesn't come to the front
		-- unless we tell it to
		activate object
		save workbook as filename "workbox3.xlsx"
	end tell
end tell
1 Like