首页 > 解决方案 > Excel 2016 for Mac 宏的运行时错误 1004(用于在 Excel 2011 for Mac 上工作)

问题描述

所以我试着环顾网络和论坛来解决这个问题,但由于我似乎根本无法理解这一点,我需要一些建议。

我有一个以前在 Mac 上的 Excel 2011 上运行的宏,但现在已经没有了。当我尝试在 Mac 上的 Excel 2016 上运行此程序时,我收到运行时错误“1004”,抱歉我们找不到(文件位置)。是否有可能它被移动,重命名等错误。

代码如下

On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")
MyScript = _
"set applescript's text item delimiters to "","" " & vbNewLine & _
           "set theFiles to (choose file of type " & _
         " {""com.microsoft.Excel.xls""} " & _
           "with prompt ""Please select a file or files"" default location alias """ & _
           MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
           "set applescript's text item delimiters to """" " & vbNewLine & _
           "return theFiles"

MyFiles = MacScript(MyScript)
On Error GoTo 0

If MyFiles <> "" Then
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    MySplit = Split(MyFiles, ",")
    For N = LBound(MySplit) To UBound(MySplit)

            X = ActiveWorkbook.Name
            Workbooks.Open (MySplit(N))

当我看到调试时,它停在代码 Workbooks.Open (MySplit(N))
它正在寻找的文件存在于该位置,因为当它提示我时我必须选择该文件

想知道是否有人可以帮助我更换什么代码谢谢

标签: excelvbaexcel-2016excel-2011

解决方案


Mac Excel 2016 VBA - Workbook.open 给出 1004 错误之后,只需修改 applescript 的输出以进行文件选择(类似于Macintosh HD:Users:xxxx:Documents:yyyy.xlsx),通过替换:/Macintosh HD使""Workbooks.Open()工作。


推荐阅读