Excel 2007 Blues Please help

crenaud

New Member
Joined
May 19, 2007
Messages
3
I'm sort of new to Excel VBA and need some help finding a work around for Application.FileSearch which is no longer supported. The idea is to fill an array with found jpeg file names for use on a form. I use the code below.

Set fs = Application.FileSearch
With fs
.LookIn = modelfolder
.filename = "*.JPG"
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
ReDim pics(.FoundFiles.Count)
NOPIC = .FoundFiles.Count

For i = 1 To .FoundFiles.Count
filename = .FoundFiles(i)
pics(i) = Mid(filename, InStrRev(filename, "\") + 1)
Next i

End If
End With

Could someone help with a solution for this in Excel 2007?
 

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Jon Peltier

MrExcel MVP
Joined
May 14, 2003
Messages
4,943
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
The function uses Dir to return an array of filenames.

Code:
Function GetFilesFromDirectory(sDirectory As String, sFilter As String) As String()
  Dim sFileName() As String
  Dim sTemp As String
  Dim i As Long
  i = 0
  
  sTemp = Dir(sDirectory & "\" & sFilter)
  
  Do While Len(sTemp) > 0
    i = i + 1
    ReDim Preserve sFileName(1 To i)
    sFileName(i) = sTemp
    sTemp = Dir()
  Loop
  
  GetFilesFromDirectory = sFileName
End Function

In your example, you call it using this:

Code:
Dim sFileNames() as String
sFileNames = GetFilesFromDirectory(modelfolder, "*.jpg")

Use sFileNames in place of .FoundFiles(i)
 

Watch MrExcel Video

Forum statistics

Threads
1,127,068
Messages
5,622,501
Members
415,901
Latest member
HermeScherme

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