search for keywords in filename

JakP

New Member
Joined
Apr 30, 2010
Messages
3
I am working on a small project and fairly new to vba, but managing alright with the guidance of many helpful people. Here's what I'm looking at.

I have a workbook that saves a sheet to a new file for each client, the new filename contains the client name, a claim number, and the date it was saved. i.e. "Jim Tate 123456 04-30-10.xls" I would like to be able to search the directory where all these files are stored for a specific keyword like "tate" or "123456"

For any client there may be more than one file (from different dates) Ultimately, I would like to search for all file that match and be able to select and open the file for viewing and/or printing.

I am working with Excel 2007 under XP Pro SP 3.

Is there an effective way to accomplish this?

Thanks for the help

JakP
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hello and welcome to the board.

Something like this perhaps?
Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] OpenFiles()
    [COLOR="Blue"]Dim[/COLOR] strPath [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] strFileName [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] strKeyword [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
 
    strKeyword = "blah" [COLOR="Green"]'enter keyword here[/COLOR]
    strPath = "C:\Excel\" [COLOR="Green"]'change path here[/COLOR]
    strFileName = [COLOR="Blue"]Dir[/COLOR](strPath & "*" & strKeyword & "*.xl*")
 
    [COLOR="Blue"]Do[/COLOR] [COLOR="Blue"]While[/COLOR] [COLOR="Blue"]Len[/COLOR](strFileName) > 0
        Workbooks.Open (strPath & strFileName)
        strFileName = [COLOR="Blue"]Dir[/COLOR]
    [COLOR="Blue"]Loop[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
That's fantasitc, thanks very much. I'm just looking around to see if I can find reference on how to create that directory listing either in a box that the user can choose the file from (in the case of multiple files) or conversely creating the list on a new sheet with a hyperlink to open a specific file..

I'm sure I'll find that in the forums. Thanks again, you've helped imeasurabley.

JakP
 
Upvote 0
Hi

This will create a list of hyperlinks of the files in column A of the active sheet.
Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] OpenFiles()
    [COLOR="Blue"]Dim[/COLOR] strPath [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] strFileName [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] strKeyword [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
 
    strKeyword = "blah" [COLOR="Green"]'enter keyword here[/COLOR]
    strPath = "C:\Excel\" [COLOR="Green"]'change path here[/COLOR]
    strFileName = [COLOR="Blue"]Dir[/COLOR](strPath & "*" & strKeyword & "*.xl*")
 
    [COLOR="Blue"]Do[/COLOR] [COLOR="Blue"]While[/COLOR] [COLOR="Blue"]Len[/COLOR](strFileName) > 0
        [COLOR="Blue"]With[/COLOR] Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Hyperlinks.Add anchor:=Range(.Address), Address:=strPath & strFileName
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
        strFileName = [COLOR="Blue"]Dir[/COLOR]
    [COLOR="Blue"]Loop[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Excellent, thank you a ton. Here I was considering a new function for creating the hpyerlink.

Thank you for the warm welcome and the great help.

JakP
 
Upvote 0

Forum statistics

Threads
1,214,946
Messages
6,122,401
Members
449,081
Latest member
JAMES KECULAH

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