Dir() in Excel for Mac 2016 causes crash

本秂侑毒 提交于 2019-12-11 17:18:40

问题


I am trying to iterate through files using Dir, and I understand that it works differently on Mac than windows. This question has been helpful, but I have been unable to implement the solution suggested. The first call to Dir successfully returns a desired filename, but the second call crashes excel. I would like to implement a pattern like this:

Sub printFileNames()
  Dim path as String
  Dim fileName as String
  path = ThisWorkbook.Path & "/SUBFOLDER/"
  fileName = Dir(path, MacID("XLSX"))
  While fileName <> ""
    MsgBox fileName
    fileName = Dir 'This CRASHES EXCEL
  Wend
End Sub

Here is a screenshot of the error message, upon crash:

Is this a known bug? Am I using Dir incorrectly for the Mac version of Excel 2016?

EDIT: I have also tried using this exact solution, posted in the above linked question:

Sub Sample()
MyDir = ActiveWorkbook.Path
strPath = MyDir & ":SUBFOLDER:"

strFile = Dir(strPath, MacID("TEXT"))

'Loop through each file in the folder
Do While Len(strFile) > 0
    If Right(strFile, 3) = "csv" Then
        Debug.Print strFile
    End If

    strFile = Dir    
Loop
End Sub

But this gives a "File Not Found" error, even though there are both .csv, .txt, and .xlsx files in the specified folder. Using:/SUBFOLDER/ instead of :SUBFOLDER: prints the appropriate file name for the first loop, but crashes upon calling strFile = Dir


回答1:


Use Ron de Bruin's macscript:

https://www.rondebruin.nl/mac/mac013.htm Loop through Files in Folder on a Mac (Dir for Mac Excel)



来源:https://stackoverflow.com/questions/44784364/dir-in-excel-for-mac-2016-causes-crash

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