'Needs a reference to Microsoft Visual Basic for Applications Extensibility
Sub XLS_Files_Search_Code_For_String()
Const cFolder = "C:Documents and SettingsOwnerMy Documents" 'there should be slashes here
Const cFile = " * .xl * """
Const cTarget = "MyView_VW"
Dim i As Long, rng As Range, wkb As Workbook, vbc As VBComponent
Dim strFile As String, lngStartLine As Long, lngStartCol As Long
Dim xCalc As XlCalculation, blnEvents As Boolean
Set rng = Selection(1) 'currently selected cell is where the results will be placed
With Application.FileSearch
.NewSearch
.LookIn = cFolder
.Filename = cFile
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
'Store old Calculation and Events state
xCalc = Application.Calculation
Application.Calculation = xlCalculationManual
blnEvents = Application.EnableEvents
Application.EnableEvents = False
On Error GoTo errwkb
For i = 1 To .FoundFiles.Count
strFile = .FoundFiles(i)
If ThisWorkbook.Path & Application.PathSeparator & ThisWorkbook.Name <> strFile Then
Set wkb = Workbooks.Open(Filename:=strFile, UpdateLinks:=0, ReadOnly:=True)
'Inspect each VB component for Target string
For Each vbc In wkb.VBProject.VBComponents
lngStartLine = 1: lngStartCol = 1
Do Until Not vbc.CodeModule.Find(cTarget, lngStartLine, lngStartCol, -1, -1)
'Target string was found
rng.Value = strFile
rng.Offset(0, 1).Value = vbc.Name
rng.Offset(0, 2).Value = lngStartLine
Set rng = rng.Offset(1, 0)
lngStartCol = lngStartCol + 1
Loop
Next
wkb.Close SaveChanges:=False
End If
reswkb:
Next
On Error GoTo 0
'Restore Calculation and Events state
Application.Calculation = xCalc
Application.EnableEvents = blnEvents
Else
MsgBox "There were no files found."
End If
End With
Exit Sub
errwkb:
rng.Value = strFile
rng.Offset(0, 1).Value = "Error: " & Err.Description
Set rng = rng.Offset(1, 0)
Resume reswkb
End Sub