Application.Filesearch conversion

johnsjen

New Member
Joined
Mar 17, 2011
Messages
4
We have just updated from Excel 2003 to 2010 and the following section of code (from a much longer macro) no longer works due to the removal of Application.Filesearch functionality. Please could someone have a look at the code below and tell me how I should rewrite it? I can read VB and write very simple bits of it but this is beyond me. I have searched the forums and had a go with both Dir and FileSystemObject but I'm afraid I don't understand enough to make it work.
Thanks in advance

'************ determine how many file dealing with ****************************************************

NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
If NewFN = False Then
' They pressed Cancel
MsgBox "Stopping because you did not select a file"
Exit Sub
End If
Workbooks.Open Filename:=NewFN, ReadOnly:=True
myfolder = CurDir
ActiveWorkbook.Close SaveChanges:=False


Set fs = Application.FileSearch
With fs
.LookIn = myfolder
.Filename = "*.xls"
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For t = 1 To .FoundFiles.Count
myfile = .FoundFiles(t)
Workbooks.Open Filename:=myfile, ReadOnly:=True
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Code:
'************ determine how many file dealing with ****************************************************
    
    NewFN = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="Please select a file")
    If NewFN = False Then
        ' They pressed Cancel
        MsgBox "Stopping because you did not select a file"
        Exit Sub
    End If

    myFolder = Left(NewFN, InStrRev(NewFN, "\"))
    'ChDrive Left(NewFN, 1)
    ChDir myFolder
    
    myFile = Dir(myFolder & "*.xls*")
    Do While myFile <> ""
        Counter& = Counter& + 1
        myFile = Dir
    Loop
    MsgBox "There were " & Counter& & " file(s) found."

    myFile = Dir(myFolder & "*.xls*")
    For t = 1 To Counter&
        Workbooks.Open Filename:=myfile, ReadOnly:=True
        myFile = Dir
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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
Back
Top