Hi all,
I have a spreadsheet which counts files in a specific windows folder using =CountFiles("C:\My Files","BMP"). I've just made up the directory on that. The number of files in the folder grows all day.
I also had to add a module to the sheet I found the code online:-
My problem is that I have to press enter on the cell formula for it to update. any ideas on how to get it to calculate when the sheet/workbook is opened.Private Function CountFiles(strDirectory As String, Optional strExt As String = "*.*") As Double
'Author : Ken Puls (www.excelguru.ca)
'Function purpose: To count files in a directory. If a file extension is provided,
' then count only files of that type, otherwise return a count of all files.
Dim objFso As Object
Dim objFiles As Object
Dim objFile As Object
'Set Error Handling
On Error GoTo EarlyExit
'Create objects to get a count of files in the directory
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder(strDirectory).Files
'Count files (that match the extension if provided)
If strExt = "*.*" Then
CountFiles = objFiles.Count
Else
For Each objFile In objFiles
If UCase(Right(objFile.Path, (Len(objFile.Path) - InStrRev(objFile.Path, ".")))) = UCase(strExt) Then
CountFiles = CountFiles + 1
End If
Next objFile
End If
EarlyExit:
'Clean up
On Error Resume Next
Set objFile = Nothing
Set objFiles = Nothing
Set objFso = Nothing
On Error GoTo 0
End Function
Bookmarks