尽管Mac OS X上的文件夹中的文件循环 - VBA Excel 2011
问题描述:
我想通过使用VBA Excel 2011在Mac OS X上的文件夹中的文件循环。我尝试了下面的代码,但它不起作用。尽管Mac OS X上的文件夹中的文件循环 - VBA Excel 2011
Sub ListAllFilesInDir()
Dim strFile As String
Dim strPath1 As String
strPath1 = ActiveWorkbook.FullName
MsgBox strPath1
strFile = Dir(strPath1)
MsgBox strFile
strFile = Dir()
MsgBox strFile
strFile = Dir()
MsgBox strFile
End Sub
当程序到达第一个MsgBox strFile
时,我得到活动工作簿的名称。我在某处读到使用Dir
而没有参数会导致文件夹中的下一个文件。但是这对我不起作用。我得到第二MsgBox strFile
命令一个空的消息框和一个错误(运行时错误5:无效的过程调用或参数”第三MsgBox strFile
命令我有4个文件的文件夹中,我通过努力环
。另外,我该怎么做只列出“.xslx”文件
答
Here's a link对dir()函数的描述你的问题是dir(strPath1)将设置dir函数来返回该EXACT文件名的所有实例如果您希望所有文件都以“.xlsx”结尾,请尝试:
dir(ActiveWorkbook.Path & application.PathSeparator & "*.xlsx")
此外,如果您有任意数量的文件要循环尝试以下代码。它的工作原理监守它返回的最后一个文件后,DIR返回一个空字符串:
Sub WorkWithFiles()
Const PATH = "C:\"
Dim file As String
file = Dir(PATH & Application.PathSeparator & "*.xlsx")
Do Until file = ""
'Run some code on the file in the file variable
Debug.Print file
'Then get the next file
file = Dir("")
Loop
End Sub
+1
通配符('*')在Mac上不起作用。请参阅Siddharth在上面给出的链接。 – rymo
如果你还在寻找一个答案,请访问以下链接:http://stackoverflow.com/questions/10045474/dir-function -not-working-in-mac-excel-2011-vba –