简体   繁体   English

使用Excel Macro将CSV导出到OS X上的〜/ Desktop

[英]Export CSV to ~/Desktop on OS X with Excel Macro

I'm trying to export a CSV using a macro in Excel on OS X (2015). 我正在尝试使用OS X(2015)上的Excel中的宏导出CSV。 The code below worked previously...but now it's failing. 下面的代码以前可以工作...但是现在失败了。 Unfortunately all the info I'm finding it unique to Windows. 不幸的是,我找到的所有信息都是Windows特有的。

I could define the exact file path but need this to work on any OS X machine so defining the users home directory is important. 我可以定义确切的文件路径,但是需要在任何OS X机器上都可以使用,因此定义用户的主目录很重要。

Sub export_csv()
    ' export csv to ~/Desktop
    user_dir = user_dir + MacScript("(do shell script ""cd ~; pwd "")")
    file_name = user_dir & "/Desktop/excel_export.csv"

    ActiveWorkbook.SaveAs Filename:=file_name, FileFormat:=xlCSV, CreateBackup:=False

End Sub

The Problem is, that i don't have any OS X to test it. 问题是,我没有任何OS X可以对其进行测试。 But i think one Problem can be if there are to many Sheets in your Workbook. 但我认为,如果您的工作簿中有很多工作表,可能是一个问题。

Sub CSV()
Dim ws As Excel.Worksheet

Dim strDirectory As String
Dim strCurWB As String
Dim longCurFor As Long

strCurWB = ThisWorkbook.FullName
longCurFor = ThisWorkbook.FileFormat
' Store current details for the workbook

'get Path for User Desktp
strDirectory = CreateObject("WScript.Shell").specialfolders("Desktop") & "\"
'Loop though each Sheet in the Workbook
For Each ws In ThisWorkbook.Worksheets
    Sheets(ws.Name).Copy
    ActiveWorkbook.SaveAs Filename:=strDirectory & ThisWorkbook.Name & "-" & ws.Name & ".csv", FileFormat:=xlCSV
    ActiveWorkbook.Close savechanges:=False
    ThisWorkbook.Activate
Next

Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=strCurWB, FileFormat:=longCurFor
Application.DisplayAlerts = True
End Sub

I hope This Code Example can Help you and you can take some over to the OSX. 希望本代码示例可以对您有所帮助,并且您可以将其应用于OSX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM