在Mac OS X-VBA Excel 2011上循环浏览文件夹中的文件
我正在尝试使用VBA Excel 2011在Mac OS X上的文件夹中循环浏览文件。我尝试了以下代码,但不起作用在Mac OS X-VBA Excel 2011上循环浏览文件夹中的文件,excel,vba,macos,Excel,Vba,Macos,我正在尝试使用VBA Excel 2011在Mac OS X上的文件夹中循环浏览文件。我尝试了以下代码,但不起作用 Sub ListAllFilesInDir() Dim strFile As String Dim strPath1 As String strPath1 = ActiveWorkbook.FullName MsgBox strPath1 strFile = Dir(strPath1) MsgBox strFile strF
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
命令有一个空消息框,第三个MsgBox strFile
命令有一个错误(运行时错误5:过程调用或参数无效)。我正在尝试循环遍历的文件夹中有4个文件
另外,如果只列出“.xslx”文件来描述dir()函数,我该怎么办呢?您的问题是dir(strPath1)将设置dir函数以返回该路径中该确切文件名的所有实例,而该路径将永远是一个文件。如果希望所有文件都以“.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
您的问题是,dir(strPath1)将设置dir函数以返回该路径中该确切文件名的所有实例,而该路径只会是一个文件。如果希望所有文件都以“.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
如果您仍在寻找答案,请参阅此链接:如果您仍在寻找答案,请参阅此链接:通配符(
*
)在Mac上不能像这样工作。请参阅上面Siddharth提供的链接。通配符(*
)在Mac上不能像这样工作。请参阅上面Siddharth提供的链接。