+ Reply to Thread
Results 1 to 14 of 14

count files in a folder automatically??

  1. #1
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    count files in a folder automatically??

    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:-
    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
    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.
    Last edited by benjii19; 02-28-2011 at 10:56 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: count files in a folder automatically??

    Try making the function volatile by adding this to the start of the function:

    Please Login or Register  to view this content.

    Remember to use code tags when posting code per the forum rules.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: count files in a folder automatically??

    Thank you for the quick reply. Not sure where to put the
    PHP Code: 
    Application.Volatile 
    as I'm not very familiar with code.

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: count files in a folder automatically??

    Just pop it at the beginning:

    Please Login or Register  to view this content.

    Dom

  5. #5
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: count files in a folder automatically??

    Volatile didn't seem to work so had a quick look for other suggestions and application.calculate seemed to do the trick.

    Now to see if I can get it to save everytime it finds new files, ie evrytime it is opened.


    Thanks again

  6. #6
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: count files in a folder automatically??

    Still no joy, I thought I'd got this working but doesn't seem so today

    I'd even added a "workbook" object and "open" procedure

    Please Login or Register  to view this content.
    This still doesn't bring the new figures through by counting the contents in the folder when excel opens. Any ideas??

  7. #7
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: count files in a folder automatically??

    Making the udf volatile will work but the workbook has to calculate for the formula to refresh.

    You could add this to the worksheet code page:

    Please Login or Register  to view this content.

    It will then calculate each time you activate the worksheet.

    Dom

  8. #8
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: count files in a folder automatically??

    Still no luck, I'm not sure I understand you Dom, with the udf being volatile. I added that code you mentioned to worksheet_active but to no avail.

    This is the first time I've ever really played about with this kind of thing. It's to try and make things easier to report on at work. So basically another person can open the sheet and see some nice graphs update as soon as it opens.


    So I have :-

    Please Login or Register  to view this content.
    Followed by:-

    Please Login or Register  to view this content.
    and finally:-

    Please Login or Register  to view this content.

    Still no luck, still have to press enter in the formula bar for the cell that needs updating

  9. #9
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: count files in a folder automatically??

    Event codes like Workbook_Open and Worksheet_Activate need to go on the relevant code pages whereas the main function code should go in a regular code module.

    This article should explain it: http://www.contextures.com/xlvba01.html#Worksheet

    Dom

  10. #10
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: count files in a folder automatically??

    Hi I have got the main code in a module

    the open code in "thisworkbook"

    and the sheet code in "sheet1"

    thanks for the link, have had a look.

  11. #11
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: count files in a folder automatically??

    But you haven't made the function volatile.

    Dom

  12. #12
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: count files in a folder automatically??

    Does that mean I need to include :-

    Please Login or Register  to view this content.
    And stick it at the start of module1 which is the countfile code???

    Sorry if this sounds like I'm a bit slow.

  13. #13
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: count files in a folder automatically??

    Just where I said before: http://www.excelforum.com/2480749-post4.html

    Dom

  14. #14
    Registered User
    Join Date
    08-04-2010
    Location
    Rotherham
    MS-Off Ver
    Excel 2003
    Posts
    49

    Re: count files in a folder automatically??

    Thankyou. It works now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1