is it possible to have multiple searches in a loop?

iknowu99

Well-known Member
Joined
Dec 26, 2004
Messages
1,158
Application.FileSearch is nested >> do they interfere? specifically the with statements




Code:
Sub withINSIDEwith()
'mock example

With Application.FileSearch
    .LookIn = "C:\abc\"
    .Filename = "*.xls"
    .FileType = msoFileTypeExcelWorkbooks
    .SearchSubFolders = True
    .Execute
    If .Execute > 0 Then
        'loop through all found files
        For IFoundFiles = 1 To .FoundFiles.Count
            
            'set incidental variables
            Pos = InStrRev(.FoundFiles(IFoundFiles), "\")
            file = Right(.FoundFiles(IFoundFiles), Len(.FoundFiles(IFoundFiles)) - Pos)
            path = Left(.FoundFiles(IFoundFiles), Pos)
            
            Set wb = Workbooks.Open(path & file)

                'do some stuff here
                '********
                'do some stuff here
                
                                    With Application.FileSearch
                                        .LookIn = "C:\jk\"
                                        .Filename = "Foo*.xls"       'a different file is found here
                                        .FileType = msoFileTypeExcelWorkbooks
                                        .SearchSubFolders = True
                                        .Execute
                                        If .Execute > 0 Then
                                            'loop through all found files
                                            For KfoundFiles = 1 To .FoundFiles.Count
                                                
                                                'set incidental variables
                                                pos2 = InStrRev(.FoundFiles(KfoundFiles), "\")
                                                file2 = Right(.FoundFiles(KfoundFiles), Len(.FoundFiles(KfoundFiles)) - pos2)
                                                path2 = Left(.FoundFiles(KfoundFiles), pos2)
                                                
                                                Set wb2 = Workbooks.Open(path2 & file2)
                                                
                                                'do more stuff here
                                                '********
                                                'do more stuff here
                                                
                                    Next KfoundFiles
                                    End If 'If .Execute > 0 Then
                                    End With

Next IFoundFiles
End If 'If .Execute > 0 Then
End With


End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Try
Code:
Sub withINSIDEwith()
Dim i As Byte, fn As String, myList
myList = Array(Array("C:\abc\","*.xls"), Array("C:jk\","Foo*.xls"))
For i = 0 To UBound(myList)
     fn = Dir(myList(i)(0) & myList(i)(1))
     Do While fn <> ""
          Set wb = Workbooks.Open(myList(i)(0) & myList(i)(1))
          ' do your stuff
          fn = Dir()
     Loop
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,191,558
Messages
5,987,274
Members
440,087
Latest member
Ruppert23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top