RoundRocket
New Member
- Joined
- Nov 28, 2013
- Messages
- 24
Below is a handy bit of code in Excel 2007 which simply lists all the files it finds in a folder in the first column of the worksheet, I'd like to somehow adapt this code to just 'count' the types of files in the folder so that it looks to a folder and reports back (see below 'desired result' for example), perhaps printing it to a .txt file instead of adding the files to a worksheet...? i cant see how to do this.
desired result:
There are 23 .docs files in the folder
There are 10 .pdf files in the folder
There are 19 .xls files in the folder
There are 120 .docx files in the folder
Any help you may be able to give on this would be awesome. Also, i do hope the code below is useful to anyone else that might be looking for a 'list files in folder' macro.
Thank you.
Sub Button3_Click()
Call ListDocumentFiles
End Sub
Sub ListDocumentFiles()
Dim fso As New FileSystemObject
Dim fle As file
Dim fldr As folder
Dim i As Integer
Call ThisWorkbook.Worksheets(2).Range("A:B").Clear
With Application.FileDialog(msoFileDialogFolderPicker)
Call .Show
Set fldr = fso.GetFolder(.SelectedItems(1))
End With
For Each fle In fldr.Files
If InStr(1, fle.Name, ".", vbTextCompare) <> 0 Then
Let i = i + 1
Let ThisWorkbook.Worksheets(2).Cells(i, 1) = fle.Name
End If
Next
MsgBox ("Search complete")
Set fle = Nothing
Set fldr = Nothing
Set fso = Nothing
End Sub
desired result:
There are 23 .docs files in the folder
There are 10 .pdf files in the folder
There are 19 .xls files in the folder
There are 120 .docx files in the folder
Any help you may be able to give on this would be awesome. Also, i do hope the code below is useful to anyone else that might be looking for a 'list files in folder' macro.
Thank you.
Sub Button3_Click()
Call ListDocumentFiles
End Sub
Sub ListDocumentFiles()
Dim fso As New FileSystemObject
Dim fle As file
Dim fldr As folder
Dim i As Integer
Call ThisWorkbook.Worksheets(2).Range("A:B").Clear
With Application.FileDialog(msoFileDialogFolderPicker)
Call .Show
Set fldr = fso.GetFolder(.SelectedItems(1))
End With
For Each fle In fldr.Files
If InStr(1, fle.Name, ".", vbTextCompare) <> 0 Then
Let i = i + 1
Let ThisWorkbook.Worksheets(2).Cells(i, 1) = fle.Name
End If
Next
MsgBox ("Search complete")
Set fle = Nothing
Set fldr = Nothing
Set fso = Nothing
End Sub