Excel 2011 VBA for Mac 中是否有 Scripting.FileSystemObject 的替代方案?

发布于 2024-10-11 22:49:01 字数 1442 浏览 3 评论 0原文

如何我可以在 Excel 2011 for MAC 中安装/使用“Scripting.FileSystemObject”吗? 似乎表明在 Excel 2010 for Mac 中使用Scripting.FileSystemObject可能的。

还有什么其他替代方案可供使用,以便我可以:

  • 获取特定目录中所有 Excel 文件的集合
  • 迭代每个文件中的每个工作表并导出它到 .csv 文件

目前,每个文件都有一个六步过程:

--how to create CSV files for all worksheets in a file:
1. open file
2. click "Developer"
3. click editor
4. click ThisWorkbook
5. copy in:
Sub save_all_csv()
    On Error Resume Next
    Dim ExcelFileName As String
    ExcelFileName = ThisWorkbook.Name
    For Each objWorksheet In ThisWorkbook.Worksheets
        Filename = "FILE-" & ExcelFileName & "-WORKSHEET-" & objWorksheet.Name & ".csv"
        objWorksheet.SaveAs Filename:="Macintosh HD:Users:edward:Documents:temporaryNoBackup:" & Filename, FileFormat:=xlCSV, CreateBackup:=False
    Next
    Application.DisplayAlerts = False
    Application.Quit
End Sub
6. click run (it closes by itself)

我正在寻找一种自动化方法理想情况下,Mac 上的一个(cron 作业?、服务?)将每 10 分钟打开一次 Excel 文件,然后依次查看目录,将所有其他 Excel 文件转换为 .csv 文件,然后自行关闭。

如果没有 Scripting.FileSystemObject,如何在 Mac 上全自动进行 Excel 到 CSV 的转换?

The answers to How can I install/use “Scripting.FileSystemObject” in Excel 2011 for MAC? seem to indicate that using Scripting.FileSystemObject in Excel 2010 for the mac is not possible.

What other alternative is available so I can:

  • get a collection of all Excel files in a specific directory
  • iterate through each worksheet within each file and export it to a .csv file

Currently this is a six-step process for each file:

--how to create CSV files for all worksheets in a file:
1. open file
2. click "Developer"
3. click editor
4. click ThisWorkbook
5. copy in:
Sub save_all_csv()
    On Error Resume Next
    Dim ExcelFileName As String
    ExcelFileName = ThisWorkbook.Name
    For Each objWorksheet In ThisWorkbook.Worksheets
        Filename = "FILE-" & ExcelFileName & "-WORKSHEET-" & objWorksheet.Name & ".csv"
        objWorksheet.SaveAs Filename:="Macintosh HD:Users:edward:Documents:temporaryNoBackup:" & Filename, FileFormat:=xlCSV, CreateBackup:=False
    Next
    Application.DisplayAlerts = False
    Application.Quit
End Sub
6. click run (it closes by itself)

I'm looking for a way to automate this on the Mac, ideally, a (cron job?, service?) would open the excel file every 10 minutes, which would in turn look in a directory, convert all the other Excel files to .csv files, and then close by itself.

Without Scripting.FileSystemObject, how can I make this Excel-to-CSV conversion fully automatic on the Mac?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相权↑美人 2024-10-18 22:49:01

我能想到的唯一方法是使用“Dir”功能。由于 mac 支持文件名中的额外字符,因此通配符不适用于“Dir”函数。这是一个示例。

Function GetFileList(folderPath As String) As Collection
'mac vba does not support wildcards in DIR function

    Dim file As String
    Dim returnCollection As New Collection

    If Right$(folderPath, 1) <> "/" Then
        folderPath = folderPath & "/"
    End If

    file = Dir$(folderPath) 'setup initial file

    Do While Len(file)
        returnCollection.Add folderPath & file
        file = Dir$
    Loop

    Set GetFileList = returnCollection
End Function

The only way I can think of is using the "Dir" function. Since mac supports extra characters in their filenames, wildcards do not work with the "Dir" function. Here is a sample.

Function GetFileList(folderPath As String) As Collection
'mac vba does not support wildcards in DIR function

    Dim file As String
    Dim returnCollection As New Collection

    If Right$(folderPath, 1) <> "/" Then
        folderPath = folderPath & "/"
    End If

    file = Dir$(folderPath) 'setup initial file

    Do While Len(file)
        returnCollection.Add folderPath & file
        file = Dir$
    Loop

    Set GetFileList = returnCollection
End Function
遗失的美好 2024-10-18 22:49:01

您可以将 VBA 放入附加到 Excel 本身(而不是工作簿)的加载项(.xlam 文件)中。对于您的示例代码,唯一的修改是针对 ActiveWorkbook 而不是 ThisWorkbook 进行编写。

Sub save_all_csv()
    On Error Resume Next
    Dim ExcelFileName As String
    ExcelFileName = ActiveWorkbook.Name
    For Each objWorksheet In ActiveWorkbook.Worksheets
        Filename = "FILE-" & ExcelFileName & "-WORKSHEET-" & objWorksheet.Name & ".csv"
        objWorksheet.SaveAs Filename:="Macintosh HD:Users:edward:Documents:temporaryNoBackup:" & Filename, FileFormat:=xlCSV, CreateBackup:=False
    Next
    Application.DisplayAlerts = False
    Application.Quit
End Sub

您还可以利用 auto_open() 自动绑定热键。完成后,您只需打开工作簿,按热键即可获取 CSV 文件。

Public Sub auto_open()

'   Register hotkeys

'   See key codes here
'   https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-onkey-method-excel

'   ^ = CTRL
'   % = ALT
'   + = SHIFT

    Application.OnKey "^+e", "save_all_csv" ' Ctrl+Shift+E will call save_all_csv()

End Sub

You can put the VBA in an add-in (.xlam file) that is attached to Excel itself, rather than the workbook. For your example code, the only modification would be to write against ActiveWorkbook instead of ThisWorkbook.

Sub save_all_csv()
    On Error Resume Next
    Dim ExcelFileName As String
    ExcelFileName = ActiveWorkbook.Name
    For Each objWorksheet In ActiveWorkbook.Worksheets
        Filename = "FILE-" & ExcelFileName & "-WORKSHEET-" & objWorksheet.Name & ".csv"
        objWorksheet.SaveAs Filename:="Macintosh HD:Users:edward:Documents:temporaryNoBackup:" & Filename, FileFormat:=xlCSV, CreateBackup:=False
    Next
    Application.DisplayAlerts = False
    Application.Quit
End Sub

You can also leverage auto_open() to automate binding a hotkey. Once that's done, you can just open a workbook, press a hotkey, and get your CSV files.

Public Sub auto_open()

'   Register hotkeys

'   See key codes here
'   https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-onkey-method-excel

'   ^ = CTRL
'   % = ALT
'   + = SHIFT

    Application.OnKey "^+e", "save_all_csv" ' Ctrl+Shift+E will call save_all_csv()

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文