![](/img/trans.png)
[英]Recursively search files for information contained in excel cell and return path
[英]Search files for information contained in excel cell and return path
我从该论坛找到了代码。 我已经非常努力地实现了结果,但是在这一部分上却失败了。 设置f = fs.GetFile(fwb)。 运行时错误'53'我正在使用Win 7,Office 2013。
我有一个Excel电子表格,其中A列中有一个文件名。A列中列出的文件名出现在一个或多个源目录中的一个或多个Ms office .doc文件中。
我需要Excel递归搜索.doc文件,并将包含A列中指定的文件名的文件的路径返回到B列。如果有多个文件,请转到C列,依此类推。
我非常需要这个宏。 请有人帮我。
__________________________________ __|______A_____|______B_____|_____ 1 | test_1.doc |c:\cost\test_1.doc| 2 | test_2.doc |c:\cost\test_2.doc|
Private Sub CommandButton1_Click()
Dim sh As Worksheet, rng As Range, lr As Long, fPath As String
Set sh = Sheets(1) 'Change to actual
lstRw = sh.Cells.Find(
What:="*",
After:=sh.Range("A1"),
LookAt:=xlPart,
LookIn:=xlFormulas,
SearchOrder:=xlByRows,
SearchDirection:=xlPrevious,
MatchCase:=False
).Row
Set rng = sh.Range("A2:A" & lstRw)
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
fPath = .SelectedItems(1)
End With
If Right(fPath, 1) <> "\" Then
fPath = fPath & "\"
End If
fwb = Dir(fPath & "*.*")
x = 2
Do While fwb <> ""
For Each c In rng
If InStr(LCase(fwb), LCase(c.Value)) > 0 Then
Worksheets("Sheet2").Range("C" & x) = fwb
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(fwb) 'Run time error '53'
Worksheets("Sheet2").Range("D" & x) = f.DateLastModified
Worksheets("Sheet2").Range("B" & x) = f.Path
Worksheets("sheet2").Range("A" & x) = c.Value
Columns("A:D").AutoFit
Set fs = Nothing
Set f = Nothing
x = x + 1
End If
Next
fwb = Dir
Loop
Set sh = Nothing
Set rng = Nothing
Sheets(2).Activate
End Sub
fwb = Dir(fPath & "*.*")
...
Set f = fs.GetFile(fwb) 'Run time error '53'
Dir(...)函数仅返回文件名(例如“ myfile.doc”),因此在调用GetFile(...)时必须在其前面添加目录路径:
Set f = fs.GetFile(fPath & fwb)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.