我需要一个代码来满足以下要求
- excel工作表的A列包含一些字符串
- 我将指定一个文件夹来搜索这些字符串
- 在该指定文件夹中,将有子文件夹和几种类型的文件,例如:.txt、.c、.xml 等。
4.我需要在整个文件夹结构中一一搜索字符串并记录所有结果,如A列中的搜索字符串在C中B文件位置的文件中出现的次数
谢谢你
下面的代码将搜索在 A 列中输入的文件名并将位置存储在 B
我尝试了以下方法:
选项显式
Dim fso As New FileSystemObject
Dim i As Long
Dim fld As Folder
Dim c As Range
Sub Find_Path()
Dim nDirs As Long, nFiles As Long, lSize As Currency
Dim sDir As String, sSrchString As String, sItem As String
Dim fldr As FileDialog
111:
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "D:\Check\" 'ActiveWorkbook.Path & "\"
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Folder to search is not selected"
GoTo 111
Else
sDir = .SelectedItems(1)
End If
End With
MsgBox "You have selected : " & sDir, vbInformation
'Application.Cursor = xlWait
Application.DisplayStatusBar = True
Application.StatusBar = "Please wait..."
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
sSrchString = Range("A" & c.Row).Value
lSize = FindFile(sDir, sSrchString, nDirs, nFiles)
If Str(nFiles) = 0 Then
Range("B" & c.Row).Value = "Not Found in the Folder : " & sDir
End If
Next
Application.Cursor = xlDefault
Application.StatusBar = False
End Sub
This will search for files in folder and sub folders. but i need to search string