Search for string using getopenfilename (code included)

Brandon M

Board Regular
Joined
Sep 18, 2015
Messages
66
Is it possible to search for a specific string while using the getopenfilename function? I'm just trying to narrow down the files in the folder to be more relevant to the user. Currently my code takes a few inputs from the user and then opens windows explorer to let them locate the files they want. But the folder contains too many irrelevant files. I'd like to allow the user to use an input box to enter a string which would allow them to see only files containing that string.

for example, say a directory contains 9 files:

RT5-CMP-8554-014-9500.txt
RT5-CMP-8554-014-9600.txt
RT5-CMP-8554-014-9800.txt
RT5-CMP-8554-014-10000.txt
RT5-CMP-8554-014-10500.txt
RT5-CMP-8554-020-9500.txt
RT5-CMP-8554-020-9600txt
RT5-CMP-8554-020-9700.txt
RT5-CMP-8554-020-11500.txt


I would like for the user to be able to enter 014 into an input box and the result would be that they only see the files containing that string:

RT5-CMP-8554-014-9500.txt
RT5-CMP-8554-014-9600.txt
RT5-CMP-8554-014-9800.txt
RT5-CMP-8554-014-10000.txt
RT5-CMP-8554-014-10500.txt


The code below seems to work properly, but there is no attempt to filter the data with anything other than .txt files. I can't find anything that even gets me close. Any help is much appreciated.

Code:
Sub ImportData()
Dim Path As String
Dim fnameandpath As Variant
Dim WBt As Workbook
Dim WBf As Workbook
Dim WSt As Worksheet
Dim WSf As Worksheet
Dim RPM As Variant
Dim DS As Variant
 
Worksheets("Data").Activate
Set WBt = ActiveWorkbook

'***********************
'Ask for Data Set and RPM site
'***********************
DS = InputBox("Please provide Data Set")
If DS = "" Then
    MsgBox ("No Data Set Provided")
    strcancel = "Y"
    Exit Sub
End If

RPM = InputBox("Please provide RPM site.")
If RPM = "" Then
    MsgBox ("No RPM site Provided")
    strcancel = "Y"
    Exit Sub
End If

' ************************************************
' Select Text File containing data
' ************************************************
strFilt = "Text files (*.txt), *.txt"
intFilterIndex = 2
strDialogueFileTitle = "Select Data Set"
ChDrive "C:"
fnameandpath = Application.GetOpenFilename(filefilter:=strFilt, FilterIndex:=intFilterIndex, Title:=strDialogueFileTitle)

If fnameandpath = False Then exitall = True

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
   
' ************************************************
' Exit If No File Selected
If fnameandpath = "" Then
    MsgBox ("No File Selected")
    strcancel = "Y"
    Exit Sub
    
ElseIf fnameandpath = "False" Then
    MsgBox ("Operation Canceled")
    strcancel = "Y"
    Exit Sub
    exitall = True
    'GoTo line1
End If
'*************************************************
'*************************************************

Application.DisplayAlerts = False
'On Error Resume Next
Workbooks.OpenText Filename:=fnameandpath, DataType:=xlDelimited, Tab:=True, Semicolon:=False, Local:=True
Set WBf = ActiveWorkbook
Set WSf = WBf.ActiveSheet

WBt.Activate
Set WSt = WBt.Sheets(RPM)
WSt.Activate
With WSt
    Range("A5:KZ10000").Clear
    .Range("A5:KZ10000").Value = WSf.Range("A1:KZ10000").Value
End With
WBf.Close savechanges:=False
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Brandon M,

Rather than the .GetOpenFilename method, you might consider using .FileDialog(msoFileDialogFilePicker)...

Code:
Sub LimitFileSelection()
Dim fd As FileDialog
Dim Surch As String

Surch = InputBox("Please enter a search term.", Title:="Optional:")

''''Open FileDialog and select file(s)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
    .InitialView = msoFileDialogViewList
    .AllowMultiSelect = True
    .InitialFileName = "*" & Surch & "*"
    If .Show = 0 Then Exit Sub
End With

End Sub

Cheers,

tonyyy
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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