VBAの再帰でフォルダA以下にあるファイルを全て取得しようと思っています
フォルダAの中に3つのファイルがある構造です
フォルダA-------ファイルA
・・・・・・・・ファイルB
・・・・・・・・ファイルC
以下のコードだとファイルAを永遠と取得しつづけてしまいます(無限ループ?)。
Do Whileの中のbufが更新されていないことが原因と考え、callの後にExit do を書いて、doを抜けて更新させようとすると、ファイルAしか取得せず処理が終わってします。
全てのファイルを取得できるようにしたいのですが、どのようにすればいいのでしょうか?
Sub FileSearch(path) Dim fso As Object, folder As Variant, file As Variant, buf As String, this As Worksheet Set fso = CreateObject("Scripting.FileSystemObject") buf = Dir(path & "*test.xls*") Do While buf <> "" ReDim Preserve Sheet(bb) ReDim Preserve Sheet_path(bb) Sheet(bb) = buf Sheet_path(bb) = path bb = bb + 1 buf = Dir() Call hikaku Loop Debug.Print buf For Each folder In fso.GetFolder(path).subFolders Call FileSearch(folder.path) Next folder End Sub
hikaku
Sub hikaku() If bb > 0 Then Do While UBound(Sheet) >= b Application.ScreenUpdating = False my = Sheet_path(b) Filename = Dir(my & "\" & "*test.xls*") Set open_file = Workbooks.Open(Filename:=my & "\" & Filename, UpdateLinks:=False) Set target_sheet = Workbooks(Filename).Worksheets("画面") Set target_sheet2 = ThisWorkbook.Worksheets("画面") MaxRow = target_sheet.Cells(Rows.Count, 2).End(xlUp).Row ReDim screen(1, 1 To MaxRow) ReDim Number(1, 1 To MaxRow) ReDim Lavel(1, 1 To MaxRow) ReDim Project_type(1, 1 To MaxRow) ReDim Control(1, 1 To MaxRow) ReDim Events(1, 1 To MaxRow) ReDim Sort(1, 1 To MaxRow) ReDim Lifting(1, 1 To MaxRow) ReDim Erea(1, 1 To MaxRow) C = 1 d = 1 h = 1 For i = 1 To UBound(screen, 1) For f = 1 To MaxRow If WorksheetFunction.IsNumber(target_sheet.Cells(d, 2).Value) = True And Not target_sheet.Cells(d, 2).Value = "" Then screen(i, h) = target_sheet.Cells(d, 4) Number(i, h) = target_sheet.Cells(d, 2) Lavel(i, h) = target_sheet.Cells(d, 14) Project_type(i, h) = target_sheet.Cells(d, 10) Control(i, h) = target_sheet.Cells(d, 32) Events(i, h) = target_sheet.Cells(d, 81) Sort(i, h) = target_sheet.Cells(d, 85) Lifting(i, h) = target_sheet.Cells(d, 87) h = h + 1 End If If TypeName(target_sheet.Cells(d, 2).Value) = "String" Then Erea(i, C) = target_sheet.Cells(d, 2) C = C + 1 End If d = d + 1 Next f Next i d = MaxRow Do While h > 1 If TypeName(target_sheet.Cells(d, 2).Value) = "String" Then C = C - 1 ElseIf WorksheetFunction.IsNumber(target_sheet.Cells(d, 2).Value) = True Then ThisWorkbook.Worksheets(1).Range("A2:L2").Insert target_sheet2.Cells(2, 6) = CStr(screen(1, h - 1)) target_sheet2.Cells(2, 2) = Workbooks(Filename).Worksheets("???").Cells(16, 25) target_sheet2.Cells(2, 3) = Workbooks(Filename).Worksheets("???").Cells(17, 25) target_sheet2.Cells(2, 5) = Number(1, h - 1) target_sheet2.Cells(2, 7) = CStr(Lavel(1, h - 1)) target_sheet2.Cells(2, 8) = CStr(Project_type(1, h - 1)) target_sheet2.Cells(2, 9) = CStr(Control(1, h - 1)) target_sheet2.Cells(2, 10) = CStr(Events(1, h - 1)) target_sheet2.Cells(2, 11) = Sort(1, h - 1) target_sheet2.Cells(2, 12) = Lifting(1, h - 1) target_sheet2.Cells(2, 4) = Erea(1, C - 1) 'koko ThisWorkbook.Worksheets(1).Range("A2:L2").ClearFormats h = h - 1 End If d = d - 1 Loop Workbooks(Filename).Close Application.ScreenUpdating = True b = b + 1 Loop Else MsgBox "なし" End End If End Sub
回答4件
あなたの回答
tips
プレビュー