Excel:search文件夹内的所有zip文件

我有一个macros( 通过使用VBA的文件夹中的文件循环? ),看起来在一个文件夹中的zip文件,但问题是,最后一个zip文件有一个空string作为名称。 我怎样才能避免这个错误。

macros的代码:

Sub LoopThroughFiles() ' Define variables Dim file As String, folder As String ' Define working directory folder = "C:\Users\cportocarrero\Desktop\Curvas\" ' Define type of file to search for file = Dir(folder & "*.zip") ' Loop through all the files Do While Len(file) > 0 Debug.Print file file = Dir MsgBox file Loop End Sub 

你必须保持这样的循环:
– 然后在循环之前先调用dir
– while循环
– 调用dir应该是循环内的最后一个命令

 file = Dir(folder & "*.zip") Do While file <> "" Debug.Print file MsgBox file file = Dir Loop 

dir返回一个空string,这意味着没有更多的匹配。