【发布时间】:2018-03-23 09:41:57
【问题描述】:
我需要在 VBA 中编写代码以在子目录中查找文件。
使用 link 中的 'brettdj' 中的代码,如果我指定完整目录,我可以找到该文件
Sub LoopThroughFiles()
Dim MyObj As Object, MySource As Object, file As Variant
file = Dir("\\A\B\C\D\")
While (file <> "")
If InStr(file, "701000034955") > 0 Then
MsgBox "found " & file
Exit Sub
End If
file = Dir
Wend
End Sub
我正在寻找不必指定完整目录的原因。
我尝试了link 中的代码,但我在最后一行收到“类型不匹配”错误消息
Sub Find_Files()
f = "\\A\B\"
ibox = "701000034955"
sn = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & f & ibox & """ /s /a /b").stdout.readall, vbCrLf)
Sheets("Sheet1").[A1].Resize(UBound(sn) + 1) = Application.Transpose(sn) ' I get an error message in this line
End Sub
关于为什么上面的代码不起作用以及是否有更好的解决方案在子文件夹中搜索文件有什么想法吗?
【问题讨论】:
标签: vba file subdirectory