1

我有一个宏(Loop through files in a folder using VBA?),它在文件夹中查找 zip 文件,但问题是最后一个 zip 文件有一个空字符串作为名称。我怎样才能避免这个错误。

宏的代码:

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
4

1 回答 1

1

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

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

dir返回一个空字符串时,这意味着没有更多的匹配项。

于 2013-05-21T22:05:11.100 回答