Sub FileSearch(path As String)
Dim FSO As Object, Folder As Variant, File As Variant
Set FSO = CreateObject("Scripting.FileSystemObject")
'サブフォルダ取得
For Each Folder In FSO.GetFolder(path).SubFolders
Call FileSearch(Folder.path)
Next Folder
file_path = "C:\Users******\Desktop****"
book = Dir(file_path & "*テスト.xls*")
a = 55
'エクセルファイル取得
For Each File In FSO.GetFolder(path).Files
If File.Name = Dir(path & "*テスト.xls*") Then
'ここで貼り付け
Workbooks(book).Worksheets("Sheet1").Cells(a, 1) = File.Name
End If
Next File
End Sub
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
回答3件
0
ExcelVBA
1Option Explicit
23'Microsoft Scripting Runtimeを参照設定して使用すること
4Dim mFSO As FileSystemObject
5Dim mRng As Range
67Sub test()
8 Dim strFolder As String
9 Dim ix As Long
1011 'フォルダー選択指定
12 If GetFolderPath(strFolder) = False Then Exit Sub
13 '書き出しセル初期値
14 Set mRng = Workbooks(Workbooks.Count).Worksheets(1).Range("A55")
15 'FSO取得
16 Set mFSO = New FileSystemObject
1718 'ファイルフルパス一覧取得
19 GetFileList mFSO.GetFolder(strFolder), ix
20End Sub
2122'フォルダの選択
23Private Function GetFolderPath(ByRef sPath As String) As Boolean
24 With Application.FileDialog(msoFileDialogFolderPicker)
25 .InitialFileName = ThisWorkbook.Path
26 .AllowMultiSelect = False
27 .Title = "フォルダの選択"
28 If .Show = True Then
29 sPath = .SelectedItems(1)
30 GetFolderPath = True
31 End If
32 End With
33End Function
3435'ファイル一覧取得(サブフォルダ含む)
36Private Function GetFileList(ByVal objFolder As Folder, ByRef i As Long) As Boolean
37 Dim fo As Folder
38 Dim fi As File
3940 For Each fo In objFolder.SubFolders
41 GetFileList fo, i
42 Next
4344 For Each fi In objFolder.Files
45 If mFSO.GetExtensionName(fi) Like "xls?" Then
46 i = i + 1
47 With mRng(i, 1)
48 .Worksheet.Hyperlinks.Add Anchor:=.Cells, _
49 Address:=fi.Path, _
50 TextToDisplay:=fi.Name
51 End With
52 End If
53 Next
54End Function
5556
僕なら
Private Function GetFileList(ByVal sPath As String, ByRef i As Long) As Boolean
のように、セルの相対位置の値を、
次の呼び出すプロシージャに受け渡していきますかね。
編集ありがとうございます
以下のようにしたのですが、プロシージャが違うのか何も書き込まれません
Sub sample()
a = 55
Call FileSearch("C:\Users\xxxxxx\Documents\Document\xxxxx")
End Sub
Sub FileSearch(path As String)
Dim FSO As Object, Folder As Variant, File As Variant, buf As String
Set FSO = CreateObject("Scripting.FileSystemObject")
buf = Dir(path & "*???.xls*")
_
For Each Folder In FSO.GetFolder(path).SubFolders
Call FileSearch(Folder.path)
Next Folder
Do While buf <> ""
a = a + 1
ThisWorkbook.Worksheets("Sheet2").Cells(a, 1) = buf
buf = Dir()
Loop
End Sub
2020/08/07 01:53 編集
2020/08/07 02:02
2020/08/07 02:04