【发布时间】:2019-02-04 02:09:44
【问题描述】:
我正在开发一个宏来输出文件名。我有一个包含工业批处理日志文件的目录。每个批次都分配有一个 5 位数的批次编号,每个批次都有一个 .csv 和 .txt 文件。两个文件的文件名相同,并包含批号,例如:
XYZ 53482 20180827.csv
XYZ 53482 20180827.txt
XYZ 53483 20180828.csv
XYZ 53483 20180828.txt
XYZ 53484 20180829.csv
XYZ 53484 20180829.txt
到目前为止我的宏是:
Sub FindBatchFile()
Dim Batch As Double
Dim DirPath As String, r As Integer
Batch = InputBox("Enter Batch Number")
DirPath = Dir("C:\Data\* " & Batch & "*", vbDirectory)
r = 1
Workbooks.Add
MsgBox (DirPath)
Do Until DirPath = ""
Cells(r, 1).Value = DirPath
MsgBox (DirPath)
r = r + 1
DirPath = Dir
Loop
End Sub
这可以正常工作,但输出包含 .csv 和 .txt 文件。有没有办法在 Dir 函数中使用多个通配符(即包括“*.csv”标准以及“*Batch*”)?
非常感谢!
【问题讨论】:
-
DirPath = Dir("C:\Data\* " & Batch & "*.csv", vbDirectory)怎么样?
标签: vba excel directory wildcard