我有一个宏(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
答案 0 :(得分:1)
你必须像这样保持循环:
- 在循环之前先调用dir
,然后再调整
- while循环
- 调用dir
应该是循环中的最后一个命令
file = Dir(folder & "*.zip")
Do While file <> ""
Debug.Print file
MsgBox file
file = Dir
Loop
当dir
返回空字符串时,这意味着没有更多匹配。