[英]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.