1

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

  1. excel工作表的A列包含一些字符串
  2. 我将指定一个文件夹来搜索这些字符串
  3. 在该指定文件夹中,将有子文件夹和几种类型的文件,例如:.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
4

1 回答 1

1

这就是您浏览文件的方式...只需为您要搜索的每个文件添加它

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
于 2013-10-16T09:21:57.467 回答