Mac Excel 2016 VBA - Workbook.open gives 1004 error

穿精又带淫゛_ 提交于 2021-02-10 05:12:55

问题


I'm trying to get a macro that works perfectly in Excel 2011 for Mac to work in Excel 2016 for mac. The goal is to have the user specify a folder with .csv files in it, and then the macro loops through all the .csvs, opening each to copy information out of it into another Workbook.

The macro fails when trying to open the first .csv file in the user-chosen folder, with a 1004 error, file cannot be found.

(side note: Earlier in the macro, workbooks.open works perfectly with a user selected FILE)

The macro is huge, so I made a brand new smaller one just to get past this problem. Here is the smaller, test code, which has the same failing behavior:

Sub Test()
Dim folderpath As Variant
Dim filename As Variant
Dim newfilename As Variant
Dim wb As Workbook
Dim newfolderpath As Variant

folderpath = MacScript("choose folder as string")

newfolderpath = Replace(folderpath, ":", "\")

MsgBox (newfolderpath)

filename = MacScript("Choose file as string")
newfilename = Replace(filename, ":", "\")
MsgBox (filename)
MsgBox (newfilename)
MsgBox (Dir(filename))
MsgBox (newfolderpath & Dir(filename))

Set wb = Workbooks.Open(newfolderpath & Dir(filename))
End Sub

All the msgboxes provide expected values.

  • newfolderpath= the whole path with \ separators.
  • filename= the whole path and file name with : separators.
  • newfilename= the whole path and file name with \ separators.
  • Dir(filename)= just the file name.
  • newfolderpath & Dir(filename))= the entire path and file name with \ separators.

Set wb line gives:

Run-time error '1004'
'Hard DRIVE\USERS\DAVE\DESKTOP\CSVS\1.CSV'

could not be found.

Clearly the file is there, openable and works.

Any help is greatly appreciated.

来源:https://stackoverflow.com/questions/43748607/mac-excel-2016-vba-workbook-open-gives-1004-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!