|
Option Explicit
'フォルダ内のファイルを検索
Private Sub ExFileSearch()
Dim i As Integer
'ファイル検索を開始
With Application.FileSearch
.NewSearch
.SearchSubFolders = False
.LookIn = "C:\picture"
.Filename = "car*.*"
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox .FoundFiles.Count & "個見つかりました。"
Else
MsgBox "見つかりませんでした"
End If
End With
End Sub
'サブフォルダ内も含めファイルを検索
Private Sub ExSubFolderFileSearch()
Dim i As Integer
'ファイル検索を開始
With Application.FileSearch
.NewSearch
.SearchSubFolders = True
.LookIn = "C:\picture"
.Filename = "car*.*"
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox .FoundFiles.Count & "個見つかりました。"
Else
MsgBox "見つかりませんでした"
End If
End With
End Sub
'特定のファイルを検索
Private Sub ExFileNameSearch()
Dim i As Integer
'ファイル検索を開始
With Application.FileSearch
.NewSearch
.SearchSubFolders = False
.LookIn = "C:\picture"
.Filename = "car.jpg"
If .Execute > 0 Then
MsgBox "見つかりました。"
Else
MsgBox "見つかりませんでした"
End If
End With
End Sub
'指定フォルダ内のExcelファイルを検索
Private Sub ExExcelFileSearch()
Dim i As Integer
' ファイル検索を開始します
With Application.FileSearch
.NewSearch
.LookIn = "c:\"
.SearchSubFolders = True
.FileType = msoFileTypeExcelWorkbooks
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
For i = 1 To .FoundFiles.Count
'ファイル名を表示する
Range("E" & i) = .FoundFiles(i)
Next i
Else
MsgBox "見つかりませんでした"
End If
End With
End Sub
Private Sub CommandButton1_Click()
'フォルダ内のファイルを検索
ExFileSearch
'サブフォルダ内も含めファイルを検索
ExSubFolderFileSearch
'特定のファイルを検索
ExFileNameSearch
'指定フォルダ内のExcelファイルを検索
ExExcelFileSearch
End Sub
|
|
|
|