<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title></title>
</head>

<body>
<pre>

○○○○○○○○○○○○○○○○○
エクセルファイルの内容を検索する

Option Explicit

Sub SearchKeyword()
    ' 検索対象ディレクトリ
    Dim inputRootFolder As String
    inputRootFolder = "C:\work"
   
    ' 検索キーワード
    Dim keyword As String
    keyword = "hoge"
   
    ' ファイル一覧の取得
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
   
    Dim fileList As Collection
    Set fileList = New Collection
    Dim folderList As Collection
    Set folderList = New Collection
   
    ' ファイルとフォルダのリストを取得
    Call GetFileAndFolderNameList(fso.GetFolder(inputRootFolder), fileList, folderList)
   
    ' 検索対象ファイルの正規表現
    Dim reFile As RegExp
    Set reFile = New RegExp
    reFile.Pattern = "\.(xls|xlsx)$"
   
    ' 検索対象のbook
    Dim inputBook As Workbook
   
    ' キーワード検索実行
    Application.ScreenUpdating = False

    Dim i As Integer
    On Error Resume Next
    For i = 1 To fileList.Count
       
        ' 検索対象ファイルかどうかをチェック
        If reFile.Test(fileList(i)) Then
           
            ' ファイルオープン
            Set inputBook = Workbooks.Open(fileList(i), ReadOnly:=True)
           
            Dim j As Integer
            For j = 1 To inputBook.Worksheets.Count
                Dim result As Range
                ' コメントを検索。値を検索する場合はLookIn:=xlValues、数式を検索する場合はLookIn:=xlFormulasとする。
                ' また、部分一致で検索。完全一致で検索する場合はLookAt:=xlWhole
                Set result = inputBook.Worksheets(j).Cells.Find(What:=keyword, LookIn:=xlComments, LookAt:=xlPart)
                If Not result Is Nothing Then
                    Debug.Print (fileList(i))
                    inputBook.Close SaveChanges:=False
                    Exit For
                End If
            Next j
           
            ' ファイルクローズ
            inputBook.Close SaveChanges:=False
        End If
    Next i
   
    Application.ScreenUpdating = True
   
    Set inputBook = Nothing
    Set reFile = Nothing
    Set fileList = Nothing
    Set folderList = Nothing
    Set fso = Nothing
   
End Sub


</pre>
</body>
