Dir 會返回匹配 pathname 的第一個文件名。若想得到其它匹配 pathname 的文件名,再一次調用Dir,且不要使用參數。如果已沒有合乎條件的文件,則 Dir 會返回一個零長度字符串 ("")。一旦返回值為零長度字符串,並要再次調用 Dir 時,就必須指定 pathname,否則會產生錯誤。
文件數確定時,用for循環;
文件數不確定,用do while。
Sub dir使用1() MyPath = ThisWorkbook.Path & "\" File = Dir(MyPath & "*.xlsx*") Debug.Print File For i = 1 To 3 File = Dir Debug.Print File Next End Sub Sub dir2() MyPath = ThisWorkbook.Path & "\" File = Dir(MyPath & "*.xlsx*") Do While File <> "" Debug.Print File File = Dir Loop End Sub