繁体   English   中英

循环浏览目录VBA中的文件

[英]Loop through files in directory VBA

我在一个目录中遍历文件,一切正常,但是当我将文件传递给模块中的另一个函数时,它变得很奇怪。 它跳过循环中检索到的第一个文件! 假设示例file的第一个循环是"File1"但是一旦它命中了copyFile (file)便将"File2"传递给该函数(该函数也存在),由于某种原因,它会在调用copyFile函数时自动增加循环。

Dim file As Variant
file = Dir("PATH TO MY DIRECTORY")
Do While Len(file) > 0
    Debug.Print file  'Here the right name is printed
    file = Dir 'file here is also correct, at the beginning of the loop it shows File1 
    copyFile (file) 'Here suddenly the next file is sent to the copyFile

Loop

我尝试定义一个字符串,将file存储在其中,然后将其传递给copyFile(stringFile)但同样发生。

你可以这样尝试吗:

Dim file As Variant
file = Dir("PATH TO MY DIRECTORY")
Do While Len(file) > 0
    Debug.Print file  'Here the right name is printed
    copyFile (file) 'Here suddenly the next file is sent to the copyFile
    file = Dir 
Loop

我认为它应该起作用。 还要看一下这里的代码: 使用VBA遍历文件夹中的文件?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM