Sub GetAllFileAndFolderNames() Dim FileName As String FileName = Dir("C:\a\", vbDirectory) Do While FileName <> "" Debug.Print FileName FileName = Dir() Loop End Sub 运行后,立即窗口中显示的是: 代码语言:javascript 代码运行次数:0 运行 AI代码解释 . .. 4duck.txt 5horse.txt b c ...
1.Dir函数:高效文件查找 Dir函数是VBA中查找文件的利器,它可以遍历文件夹中的所有文件。 使用Dir(文件夹)重新返回该文件夹的第一个文件名。 使用Dir()继续在同一文件夹中查找下一个文件名。 当Dir函数返回空字符串时,代表所有文件名都已经被找到。 Dim folderPath As String, fileName As String folderPath =...
方法1:使用Dir函数 Sub LoopAllFilesInAFolder() '遍历文件夹中的所有文件 Dim fileName As Variant fileName =Dir("D:\excelperfect\") While fileName <> "" '插入对每个文件要执行的操作 '本示例代码打印文件名到立即窗口 Debug.Print fileNa...
fileName = Dir(folderPath &"*.*", vbDirectory) While Len(fileName) <> 0 If Left(fileName, 1) <>"." Then fullFilePath = folderPath &fileName If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then ReDim Preserve folders(0 TonumFolders) As String folders(numFolders) =fullFileP...
(1, 1) = "文件名" .Cells(1, 2) = "修改日期" i = 2 Do While file <> "" And file <> ThisWorkbook.Name .Cells(i, 1) = file .Cells(i, 2) = FileDateTime(path & file) i = i + 1 '一定要 不然死循环 file = Dir Loop End With '自动适应列宽 Columns("A:B").AutoFit End...
vbDirectory) Do While MyFile <> "" If MyFile <> "." And MyFile <> ".." Then If (GetAttr(brr(i) & MyFile) And vbDirectory) = vbDirectory Then d.Add (brr(i) & MyFile & "\"), "" End If MyFile = Dir Loop If Zi = False Then Exit Do ...
DIR加循环的方法,速度飞快。下面是三种方法的代码: 1、filesearch法 Sub test3() Dim wb As Workbook Dim i As Long Dim t t = Timer With Application.FileSearch '调用fileserch对象 .NewSearch '开始新的搜索 .LookIn = ThisWorkbook.path '设置搜索的路径 .SearchSubFolders = True '搜索范围...
file = Dir("c:\testfolder\") While (file&...
With Application.FileDialog(msoFileDialogFolderPicker)If .Show <> -1Then Exit Function fod = .InitialFileName End With a.DosInput Environ$("comspec") & " /c dir " & Chr(34) & fod & "\*.doc*" & Chr(34) & " /s /b /a:-d"arr = a.DosOutPutEx '默认等待时间120s arr = S...
and 与 or 或 & 连接变量和字符串,前后有空格 <> 不等于 in 在什么里 like 可使用通配符 *任意个字符 ?一个字符 #一个数字 [A-W a-w]a-w或A-W中的一个字符 [!0-9] 字符不是0-9之间 变量 DimxAsInteger整数DimstAsString文本DimrgAsRange 对象Setrg = Range("A1") ·对象赋值Dimarr(1to10...