'--------------------------------------------------------
' 指定フォルダ内の全てのブックから任意の文字列を検索する
'--------------------------------------------------------
' https://excel.syogyoumujou.com/vba/find_allbooks.html
'--------------------------------------------------------
Sub searchAllBooksForAnyString() 'メイン
'--------------------------------
' 検索する文字列を配列として設定
'--------------------------------
Dim varArray As Variant
varArray = Array("富山", "神奈川") '検索文字列
'--------------------------------
' フォルダの選択
'--------------------------------
Dim strFolderPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = True Then strFolderPath = .SelectedItems(1)
End With
If Len(strFolderPath) = 0 Then Exit Sub
'--------------------------------
' フォルダの存在確認
'--------------------------------
If Dir(strFolderPath, vbDirectory) = "" Then
MsgBox "対象のフォルダが見つかりません", vbExclamation, "終了します"
Exit Sub
End If
'--------------------------------
' フォルダ内ブックを検索
'--------------------------------
Dim strFileName As String
strFolderPath = strFolderPath & Application.PathSeparator 'フォルダパスに区切り文字追加
strFileName = Dir(strFolderPath & "*.xls?") 'フォルダからExcelブックを検索
If strFileName = "" Then 'ブックのパスを取得できなければ終了
MsgBox "指定フォルダ内にExcelブックが見つかりません", vbExclamation, "終了します"
Exit Sub
End If