Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel:搜索文件夹中的所有zip文件_Excel_Vba - Fatal编程技术网

Excel:搜索文件夹中的所有zip文件

Excel:搜索文件夹中的所有zip文件,excel,vba,Excel,Vba,我有一个宏()在文件夹中查找zip文件,但问题是最后一个zip文件的名称是空字符串。我怎样才能避免那个错误呢 宏的代码是: Sub LoopThroughFiles() ' Define variables Dim file As String, folder As String ' Define working directory folder = "C:\Users\cportocarrero\Desktop\Curvas\" ' Define t

我有一个宏()在文件夹中查找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

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

file = Dir(folder & "*.zip")
Do While file <> ""
    Debug.Print file
    MsgBox file
    file = Dir
Loop
file=Dir(文件夹&“*.zip”)
文件“”时执行此操作
调试.打印文件
MsgBox文件
file=Dir
环

dir
返回一个空字符串时,这意味着不再有匹配项。

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

file = Dir(folder & "*.zip")
Do While file <> ""
    Debug.Print file
    MsgBox file
    file = Dir
Loop
file=Dir(文件夹&“*.zip”)
文件“”时执行此操作
调试.打印文件
MsgBox文件
file=Dir
环

dir
返回一个空字符串时,这意味着不再有匹配项。

+1我在这里很挑剔,但是对
dir
的调用应该是循环中的最后一个命令,但不必是。它只需要在使用完
文件
变量之后才行。是的,您完全正确+1为了挑剔,我编辑了它。+1我在这里挑剔,但是对
Dir
的调用应该是循环中的最后一个命令,但不一定是。它只需要在使用完
文件
变量之后才行。是的,您完全正确+1由于挑剔,我对其进行了编辑。您的问题是您正在调试。打印
文件
的一个值,但在发送到MsgBox之前,您正在更改
文件
变量。KekuSemau的回答说明了循环应该如何构造。您的
While
测试不需要更改,因为
Len(file)>0
将与
file”“
具有相同的效果。您的问题是您正在调试。打印
file
的一个值,但在发送到MsgBox之前,您正在更改
file
变量。KekuSemau的回答说明了循环应该如何构造。您的
While
测试不需要更改,因为
Len(file)>0
将与
file”“