Prompt to select certain files within a folder

drom

Active Member
Joined
Mar 20, 2005
Messages
400
Hi and thanks in advance!

Imagine I have a Path and a String:

  • wPath = "C:\Users\MrExcel\Desktop\Works"
  • wName= "France"

I would like to prompt the user to select any Excel file within the Path = wPath containing the Name = wName


I know How to prompt the user to select within the Folder wPath only excel files , but I do not know how to show only the files containing the name = wName

So if the wPath has 50 Excel Files and only 4 of them containing the string "France"
eg:

  • "2018-08-05 France Sales"
  • "2018-10-11 France Customers"
  • "Sales from France"
  • "customers in France"

When prompting the User only show these Files
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,348
Not really clear on "show users only these files. Here's a start that seems like it should work...
Code:
Private Sub test()
Dim FSO As Object, FolDir As Object, FileNm As Object, WName As String
On Error GoTo Erfix
Set FSO = CreateObject("scripting.filesystemobject")
Set FolDir = FSO.GetFolder("C:\Users\MrExcel\Desktop\Works")
WName = "France"

Application.ScreenUpdating = False
For Each FileNm In FolDir.Files
If InStr(FileNm.Name, WName) Then
MsgBox FileNm.Name
'Workbooks.Open FileName:=FileNm
'Workbooks(FileNm.Name).Close SaveChanges:=False
End If
Next FileNm
Application.ScreenUpdating = True
Set FolDir = Nothing
Set FSO = Nothing
Exit Sub
Erfix:
On Error GoTo 0
MsgBox "Error"
Application.ScreenUpdating = True
Set FolDir = Nothing
Set FSO = Nothing
End Sub
HTH. Dave
 

drom

Active Member
Joined
Mar 20, 2005
Messages
400
I do not want to know how many files contain the String "France" within a Folder

  • I know how to do so

I would like to prompt the user to select a file inside a folder (I do know this) , but only show the files with the string France.

so stead of giving me the option to see 50 files within the folder I would like to see only 4 (according to my criteria= France)
And once I have only my 4 options... pick one of them

Probably this is not possible, but who knows



Not really clear on "show users only these files. Here's a start that seems like it should work...
Code:
Private Sub test()
Dim FSO As Object, FolDir As Object, FileNm As Object, WName As String
On Error GoTo Erfix
Set FSO = CreateObject("scripting.filesystemobject")
Set FolDir = FSO.GetFolder("C:\Users\MrExcel\Desktop\Works")
WName = "France"

Application.ScreenUpdating = False
For Each FileNm In FolDir.Files
If InStr(FileNm.Name, WName) Then
MsgBox FileNm.Name
'Workbooks.Open FileName:=FileNm
'Workbooks(FileNm.Name).Close SaveChanges:=False
End If
Next FileNm
Application.ScreenUpdating = True
Set FolDir = Nothing
Set FSO = Nothing
Exit Sub
Erfix:
On Error GoTo 0
MsgBox "Error"
Application.ScreenUpdating = True
Set FolDir = Nothing
Set FSO = Nothing
End Sub
HTH. Dave
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
How about
Code:
Dim fname As String
With Application.FileDialog(3)
   .InitialFileName = "C:\Users\MrExcel\Desktop\Works\*france*"
   .AllowMultiSelect = False
    If .Show = -1 Then fname = .SelectedItems(1)
End With
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,348
drom my only thoughts are that U create a userform with a listbox that populates with the file names of interest. Opening the files would then occur with a listbox selection. I don't think U can adjust the Application.FileDialog filter to screen out parts of file names... but I've been wrong many times before. Dave
 

Forum statistics

Threads
1,082,305
Messages
5,364,401
Members
400,800
Latest member
germanpbv

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top