【发布时间】:2015-09-11 12:34:20
【问题描述】:
我有一个宏 (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
【问题讨论】:
-
您的问题是您正在调试。打印
file的一个值,但是您在发送到 MsgBox 之前更改了file变量。 KekuSemau 的回答显示了您的循环应该如何构建。您的While测试不需要更改,因为Len(file) > 0将具有与file <> ""相同的效果