VBA Search files Excel 2007

myworld

New Member
Joined
Dec 8, 2009
Messages
5
Hi,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
I have a problem trying to find or work out how to do a word search from an excel 2007 sheet into a bunch of files in a folder.
<o:p> </o:p>
Basically I want to be able to write a word in an excel(2007) cell, run some VBA that searched thought a bunch of excel file, then return the file names in another column as hyperlinks to the files that the word was found in.
<o:p> </o:p>
The excel 2003 VBA I’ve been using is below, it doesn’t work in 2007, our work is extremely busy these days and I’m running out of time to figure it out.
<o:p> </o:p>
Thanks
<o:p> </o:p>
Sub NameSearch()<o:p></o:p>
Dim fs As FileSearch<o:p></o:p>
<o:p> </o:p>
Dim lLen As Long, i As Long<o:p></o:p>
Dim parts<o:p></o:p>
<o:p> </o:p>
Set fs = Application.FileSearch<o:p></o:p>
fs.NewSearch<o:p></o:p>
With fs<o:p></o:p>
With .PropertyTests<o:p></o:p>
.Add _<o:p></o:p>
Name:="Text or Property", _<o:p></o:p>
Condition:=msoConditionIncludesPhrase, _<o:p></o:p>
Value:=Range("m11")<o:p></o:p>
End With<o:p></o:p>
.LookIn = ThisWorkbook.Path & "\Files"<o:p></o:p>
.SearchSubFolders = False<o:p></o:p>
.Filename = "*"<o:p></o:p>
.MatchTextExactly = False<o:p></o:p>
.FileType = msoFileTypeAllFiles<o:p></o:p>
End With<o:p></o:p>
ActiveSheet.Unprotect Password:="1"<o:p></o:p>
Range("o1:p5000").Select<o:p></o:p>
Selection.ClearContents<o:p></o:p>
If fs.Execute() > 0 Then<o:p></o:p>
For i = 1 To fs.FoundFiles.Count<o:p></o:p>
parts = <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:eek:ffice:smarttags" /><st1:City w:st="on"><st1:place w:st="on">Split</st1:place></st1:City>(Trim(Replace(Dir(fs.FoundFiles(i)), ".xls", "")))<o:p></o:p>
Cells(i, 15) = parts(UBound(parts))<o:p></o:p>
Cells(i, 16).FormulaR1C1 = "=Hyperlink(" & Chr(34) & fs.FoundFiles(i) _<o:p></o:p>
& Chr(34) & ",R[0]C[-1])"<o:p></o:p>
Next i<o:p></o:p>
Range("p1:p5005").Select<o:p></o:p>
With Selection.Font<o:p></o:p>
.Name = "Arial"<o:p></o:p>
.Size = 14<o:p></o:p>
.Strikethrough = False<o:p></o:p>
.Superscript = False<o:p></o:p>
.Subscript = False<o:p></o:p>
.OutlineFont = False<o:p></o:p>
.Shadow = False<o:p></o:p>
End With<o:p></o:p>
Columns("o:q").Select<o:p></o:p>
Selection.Sort Key1:=Range("q1"), Order1:=xlDescending, Header:=xlGuess, _<o:p></o:p>
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _<o:p></o:p>
DataOption1:=xlSortNormal<o:p></o:p>
Range("c11").Select<o:p></o:p>
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="1"<o:p></o:p>
Range("d11:E11").Select<o:p></o:p>
Else<o:p></o:p>
Range("d11:E11").Select<o:p></o:p>
MsgBox "There were no files found."<o:p></o:p>
End If<o:p></o:p>
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
FileSearch isn't availablke in XL07, so one way may be to use FSO.

In the VBE go Tools > References and check 'Microsoft Scripting Runtime'.

Then the following can be used to loop thru the directory:

Code:
Dim fso As Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fil As Scripting.File
 
Set fso = New Scripting.FileSystemObject
Set fld = fso.GetFolder("C:\")
 
For Each fil In fld.Files
    'test that fil.Name contains 'xyz'
    'insert text & hyperlink
Next fil

This question is raised quite frequently so if you do a search you will find plenty of examples and other ways of doing this. I haven't looked at the rest of your code (please use CODE tags in future - see my signature) so you need to give it some thought and see how you can integrate.

Good luck :)
 
Upvote 0
Thanks a lot,but I can’t get it going. Yes there are many queries on the net about the search I’m after but I’m yet to find one that answers for the “run a search from a cell and return results as hyperlinks”. I can find many threads running years back but with out an answer. It sucks
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
If you could help more, it would be great
 
Upvote 0

Forum statistics

Threads
1,213,537
Messages
6,114,216
Members
448,554
Latest member
Gleisner2

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