2011-09-03 59 views
2

我想通過使用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」文件

+2

如果你還在尋找一個答案,請訪問以下鏈接:http://stackoverflow.com/questions/10045474/dir-function -not-working-in-mac-excel-2011-vba –

回答

-3

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