2013-10-16 24 views
1

我需要一个代码,以满足以下要求VBA要找到在A列的字符串中的特定文件夹中包含多种类型的文件

  1. 一个Excel工作表的列A包含一些字符串
  2. 我会指定文件夹搜索这些字符串
  3. 在指定的文件夹,将有子文件夹,以及多种类型的文件,例如:.TXT,.C,.XML等。

4,我需要搜索字符串一个接一个在整个fo lder连续结构和日志都喜欢在A列的howmany次数 搜索字符串在B档位置的文件(S)在C结果

谢谢

下面的代码将搜索文件名在列A和卖场进入在B中的位置

我尝试以下:

Explicit选项

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 
+0

使用Google。这并不是您可以在不显示您尝试过的情况下询问代码的地方。 – Manu

+0

对不起,它看起来像你没有做任何事情......所以,你的代码中缺少什么? – Manu

+0

这里的代码只搜索我在列A中输入的文件(带有扩展名)。我想在每个文件中的一个路径下搜索它 –

回答

1

这是h您可以通过一个文件...只需将其添加到您要搜索的每个文件中

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 
+1

请将它作为注释发布。这不是一个答案。 –

+0

编辑它,现在它是一个答案;) – Manu

+0

这段代码很好。但它会搜索整行匹配项。我需要它作为行中的任何地方..感谢支持 –

相关问题