可以在VBA Dir函数中使用多个通配符吗?
我正在使用宏来输出文件名。我有一个包含工业批处理日志文件的目录。每个批次都分配了一个5位数的批号,每个批次都有一个.csv和.txt文件。两个文件的文件名相同,并且包含批号,例如:可以在VBA Dir函数中使用多个通配符吗?,vba,excel,directory,wildcard,Vba,Excel,Directory,Wildcard,我正在使用宏来输出文件名。我有一个包含工业批处理日志文件的目录。每个批次都分配了一个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 Find
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
”标准以及“*批处理*
”)
非常感谢 我相信以下内容将按照您的预期工作,只需在您的DirPath中添加
.csv
:
Sub FindBatchFile()
Dim Batch As Double
Dim DirPath As String, r As Integer
Batch = InputBox("Enter Batch Number")
DirPath = Dir("C:\Data\* " & Batch & "*.csv", 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
怎么样
DirPath=Dir(“C:\Data\*”&Batch&“*.csv”,vbDirectory)
?非常感谢Xabier!这是一个非常简单的解决方案,但效果非常好。感谢您抽出时间回复