我正在处理一个宏来输出文件名。我有一个目录,其中包含工业批处理的日志文件。每个批次都分配了一个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*")?
首先要感谢大家!
发布于 2018-08-29 18:16:31
我相信下面的代码会像你期望的那样工作,只是在你的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 Subhttps://stackoverflow.com/questions/52074858
复制相似问题