--------------------------
Dim fold, fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.GetFolder("C:aaaa")
Label1.Caption= "파일개수 : " & fold.Files.Count
Set fold = Nothing
Set fso = Nothing
--------------------------
위의 예는 FileSystemObject라는 것을 이용하여 파일 개수를 구하는 방법입니다.
Option Explicit
Sub Test()
Dim FsPec As String
Dim AryA() As Variant
FsPec = ThisWorkbook.Path
FolderList FsPec, AryA '1번폴더의 폴더경로명으로 서브프로시져 호출
Cells(21, 1).Resize(UBound(AryA, 2), UBound(AryA, 1)) = Application.Transpose(AryA)
'A21셀에 각폴더의 파일갯수를 담은 배열을 뿌린다
End Sub
Sub FolderList(FsPecs As String, Ary)
Dim Fs, Fd, Fa, Fb, Fsb, FL
Dim n As Integer
Set Fs = CreateObject("Scripting.FileSystemObject") '파일시스템 오브젝트설정
Set Fd = Fs.GetFolder(FsPecs) '폴더 지정
Set Fsb = Fd.SubFolders '원폴더안의 서브폴더 지정
Set FL = Fd.Files '파일개체지정
n = 1
ReDim Preserve Ary(1 To 2, 1 To n) '배열에 담기위해서 배열크기조정
Ary(1, n) = Fd.Name '첫번째 1번 폴더의 폴더 이름을 배열에 담는다
FCount FL, Fa, Ary, n '폴더내의 엑셀파일의 갯수를 세는 서브프로시져 호출
For Each Fb In Fsb '서브폴더를 순환하면서 각각의 폴더내에 파일갯수 카운트
n = n + 1
ReDim Preserve Ary(1 To 2, 1 To n)
Ary(1, n) = Fb.Name
Set FL = Fb.Files
FCount FL, Fa, Ary, n '서브폴더내의 파일갯수 카운트하는 프로시져 호출
Next
End Sub
Sub FCount(FL, Fa, Ary, n) '폴더내의 파일갯수 카운트 하는 프로시져
For Each Fa In FL
If InStr(Fa.Name, ".xls") Then
Ary(2, n) = Ary(2, n) + 1
End If
Next
End Sub
