Excel for Mac 2016中的Dir()导致崩溃 [英] Dir() in Excel for Mac 2016 causes crash
问题描述
我正在尝试使用Dir遍历文件,并且我了解它在Mac上与Windows上的工作方式不同. 这个问题很有帮助,但是我一直在无法实施建议的解决方案.第一次调用Dir成功返回所需的文件名,但是第二次调用使excel崩溃.我想实现这样的模式:
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:
这是一个已知的错误吗? Mac版本的Excel 2016是否正确使用了Dir?
Is this a known bug? Am I using Dir incorrectly for the Mac version of Excel 2016?
我还尝试过使用上述链接问题中发布的确切解决方案:
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
但是,即使指定文件夹中同时存在.csv,.txt和.xlsx文件,也会出现找不到文件"错误.使用:/SUBFOLDER/
代替:SUBFOLDER:
会为第一个循环打印适当的文件名,但是在调用strFile = Dir
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
推荐答案
使用Ron de Bruin的脚本:
Use Ron de Bruin's macscript:
https://www.rondebruin.nl/mac/mac013.htm 在Mac上的文件夹中循环浏览文件(适用于Mac Excel的Dir)
https://www.rondebruin.nl/mac/mac013.htm Loop through Files in Folder on a Mac (Dir for Mac Excel)
这篇关于Excel for Mac 2016中的Dir()导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!