可以在 VBA Dir 函数中使用多个通配符吗?
Can you use multiple wildcards in the VBA Dir function?
我正在开发一个宏来输出文件名。我有一个包含工业批处理过程日志文件的目录。每个批次都分配有一个 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 中添加了 .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
我正在开发一个宏来输出文件名。我有一个包含工业批处理过程日志文件的目录。每个批次都分配有一个 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 中添加了 .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