0

I am working on something and I got stuck. I want to look for all xl files in my folder, and inside the subfolders of the folder, and look for a string, example "bbb" and prints all the files and cells where the string was found.

for example, I have a folder named "bla", inside three xl files, and also another folder "bla2", with 4 more xl files. it looks for "bbb" in all the files, and prints a new worksheet with the path of the file and the cell that match.

So, almost everything works, just it runs in one of my loops to many times, so it prints duplicate values.

Here is the code:

Sub SearchFolders()
Dim fso As Object
Dim strSearch As String
Dim strFile As String
Dim wOut As Worksheet
Dim wbk As Workbook
Dim wks As Worksheet
Dim lRow As Long
Dim rFound As Range
Dim strFirstAddress As String
Dim oFolder, oSubfolder, oFile, queue As Collection
Dim HostFolder As String

HostFolder = "C:\Users\a\Desktop\xl files"

On Error GoTo ErrHandler
Application.ScreenUpdating = False

strSearch = "bbb" 'the text to match

Set wOut = Worksheets.Add
lRow = 1
With wOut
    .Cells(lRow, 1) = "Workbook"
    .Cells(lRow, 2) = "Worksheet"
    .Cells(lRow, 3) = "Cell"
    .Cells(lRow, 4) = "Text in Cell"

    'now some iterations through subfolders and folders
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set queue = New Collection
    queue.Add fso.GetFolder(HostFolder)

    Do While queue.Count > 0
        Set oFolder = queue(1)
        queue.Remove 1 'dequeue

        For Each oSubfolder In oFolder.SubFolders
            queue.Add oSubfolder 'enqueue
        Next oSubfolder
        For Each oFile In oFolder.Files
            strFile = Dir(oFolder & "\*.xls*")
            '**********************************************************************
            Do While strFile <> "" '***THIS IS THE LOOP WHERE I THINK THE PROBLAM IS
                Set wbk = Workbooks.Open _
                  (Filename:=oFolder & "\" & strFile, _
                  UpdateLinks:=0, _
                  ReadOnly:=True, _
                  AddToMRU:=False)

                For Each wks In wbk.Worksheets
                    Set rFound = wks.UsedRange.Find(strSearch, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
                    If Not rFound Is Nothing Then
                        strFirstAddress = rFound.Address
                    End If
                    Do
                        If rFound Is Nothing Then
                            Exit Do
                        Else
                            lRow = lRow + 1
                            .Cells(lRow, 1) = oFolder & "\" & strFile
                            .Cells(lRow, 2) = wks.Name
                            .Cells(lRow, 3) = rFound.Address & temp
                            .Cells(lRow, 4) = rFound.Value
                        End If
                        Set rFound = wks.Cells.FindNext(After:=rFound)
                    Loop While strFirstAddress <> rFound.Address
                Next

                wbk.Close (False)
                strFile = Dir
            Loop

        Next oFile
    Loop


    .Columns("A:D").EntireColumn.AutoFit
End With
MsgBox "Done"
ExitHandler:
Set wOut = Nothing
Set wks = Nothing
Set wbk = Nothing
Set oFolder = Nothing
Set oSubfolder = Nothing
Set oFile = Nothing
Set queue = Nothing
Set fso = Nothing
Application.ScreenUpdating = True
Exit Sub

ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler

End Sub
pnuts
  • 58,317
  • 11
  • 87
  • 139
dani jinji
  • 451
  • 3
  • 11
  • 21
  • Take a look at [Cycle through sub-folders and files in a user-specified root directory](http://stackoverflow.com/questions/14245712/cycle-through-sub-folders-and-files-in-a-user-specified-root-directory) for a good way of getting each file. Then you just need to open each file and check it for the string. – Ron Rosenfeld Nov 11 '15 at 17:18

1 Answers1

0
For Each oFile In oFolder.Files
   strFile = Dir(oFolder & "\*.xls*")
   Do While strFile <> "" '***THIS IS THE LOOP WHERE I THINK THE PROBLAM IS
      ............
      strFile = Dir
   Loop
Next oFile

Indeed, you have found where the error is. You dont need two nested loops here, that is what generates the duplicates. You should use either of the two techniques (using Dir or oFolder.Files collection), but not both.

A quick fix of your code would be to use only the insider loop:

strFile = Dir(oFolder & "\*.xls*")
Do While strFile <> ""
    ......... ' <~~ leave insider code as is
    strFile = Dir
Loop
A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • thank you though it still has a problam, becuase now if I have other file foramts in the folders, such as word docs, it doesn't skip them, it just says that cant open file. – dani jinji Nov 11 '15 at 17:11
  • @danijinji I was editing the code, I think you took it before I edit it. Please see the edited version :) – A.S.H Nov 11 '15 at 17:13
  • thank you very much, works perfect! is there a difference in time run bitween the two? or do you have any suggestions for better run time? – dani jinji Nov 11 '15 at 17:23
  • There shouldn't be any difference performance difference to care about. You are welcome. – A.S.H Nov 11 '15 at 17:25