How to search files from a directory with two keywords in file name???

aaromic2000

New Member
Joined
Jul 28, 2009
Messages
32
I have a shared drive mapped to directory "M:\\"

I need to search a file from one of the folders in the directory. I'm able to list all the files from a folder in a excel sheet by specifying the path in the program. But I don't know how to search a individual file in the folder with two keywords in the file name...

Can anyone help me out.../
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

bertie

Well-known Member
Joined
Jun 12, 2009
Messages
1,869
Here is one way.

Set up your key words and the path to the folder:
Code:
  [COLOR=red]key1[/COLOR] = "Boo"
 [COLOR=red] key2[/COLOR] = "k1"
 
  sPath = "c:\temp\"

Get the first Excel file:
Code:
  sFilename = Dir(sPath & "*.xl*")

Loop through all the Excel files in the folder and use the InStr() function to test the filename
Code:
  [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] sFilename = ""
    [COLOR=darkblue]If[/COLOR] InStr(1, sFilename, [COLOR=red]key1[/COLOR], vbTextCompare) Or _
      InStr(1, sFilename, [COLOR=red]key2[/COLOR], vbTextCompare) [COLOR=darkblue]Then[/COLOR]
 
      MsgBox "Found one"
 
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    sFilename = Dir()
  [COLOR=darkblue]Loop[/COLOR]

Putting it all together:

Code:
[COLOR=darkblue]Sub[/COLOR] test()
  [COLOR=darkblue]Dim[/COLOR] sPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] sFilename [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] key1 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
  [COLOR=darkblue]Dim[/COLOR] key2 [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
 
  key1 = "Boo"
  key2 = "k1"
 
  sPath = "c:\temp\"
 
  [COLOR=darkblue]If[/COLOR] Right(sPath, 1) <> "\" [COLOR=darkblue]Then[/COLOR] sPath = sPath & "\"
 
  sFilename = Dir(sPath & "*.xl*")
 
  [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] sFilename = ""
    [COLOR=darkblue]If[/COLOR] InStr(1, sFilename, key1, vbTextCompare) Or _
      InStr(1, sFilename, key2, vbTextCompare) [COLOR=darkblue]Then[/COLOR]
 
      MsgBox "Found one"
 
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    sFilename = Dir()
  [COLOR=darkblue]Loop[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps,
Bertie
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,610
Messages
5,765,412
Members
425,285
Latest member
andypandypoo

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