首页 > 技术文章 > VBA中Dir函数的使用

redufa 2020-09-14 01:45 原文

Dir 会返回匹配 pathname 的第一个文件名。若想得到其它匹配 pathname 的文件名,再一次调用Dir,且不要使用参数。如果已没有合乎条件的文件,则 Dir 会返回一个零长度字符串 ("")。一旦返回值为零长度字符串,并要再次调用 Dir 时,就必须指定 pathname,否则会产生错误。

 

文件数确定时,用for循环;

文件数不确定,用do while。

 

Sub dir使用1()


MyPath = ThisWorkbook.Path & "\"

File = Dir(MyPath & "*.xlsx*")
Debug.Print File

For i = 1 To 3
File = Dir
Debug.Print File
Next


End Sub

Sub dir2()


MyPath = ThisWorkbook.Path & "\"
 
File = Dir(MyPath & "*.xlsx*")

Do While File <> ""


Debug.Print File

File = Dir
Loop


End Sub

 

推荐阅读