VBA在特定文件夹内的列A中查找string包含几种types的文件

我需要一个代码来满足下面的要求

  1. Excel表格的A列包含一些string
  2. 我将指定一个文件夹来search这些string
  3. 在指定的文件夹中,将会有子文件夹和几种types的文件,例如:.txt,.c,.xml等等。

4.需要在整个文件夹结构中逐一searchstring,并在列A中loggingsearchstring的所有结果。如何在C中的B文件位置的文件中出现多个文件

谢谢

下面的代码将search在列A中input的文件名并将该位置存储在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 

这是你如何可以通过一个文件…只要将其添加到您要search的每个文件

 Dim filenum, targetfile, Line filenum = FreeFile targetfile = "C:\Mytextfile.txt" Open targetfile For Input As filenum Do While Not EOF(filenum) Input #filenum, Line 'if InStr(1, Line, yourSearchString) then 'check if your string is in this line Loop Close filenum