+ Reply to Thread
Results 1 to 5 of 5

Macro lists files in folders & subfldrs - Need to add ability for user to select folder

  1. #1
    Registered User
    Join Date
    07-26-2012
    Location
    Jacksonville Beach, FL
    MS-Off Ver
    Excel 2010
    Posts
    3

    Cool Macro lists files in folders & subfldrs - Need to add ability for user to select folder

    Hello Everyone -

    I have borrowed some code I found here:
    http://www.exceltip.com/st/List_file...Excel/446.html
    that lists all files in a given folder and also all files within any subfolders underneath the given folder. I would like to modify this code to:

    1-Allow the user to select the folder from which to list all files using some sort of dialogue box
    2-Allow the user to select a date and only list files created after that date

    I am using Excel 2010. Here is the code (I have made a few slight modifications for my purposes):

    Please Login or Register  to view this content.
    I am guessing I need to utilize Application.FileDialog(msoFileDialogFolderPicker) in some way for the folder selection part, but I am not sure how to incorporate it.

    I have also attached a workbook with my code including my attempt at adding the DialogFolderPicker.

    Any help or guidance anyone could provide would be greatly appreciated!
    Attached Files Attached Files
    Last edited by BeachBum08; 07-26-2012 at 03:22 PM. Reason: clarification

  2. #2
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro lists files in folders & subfldrs - Need to add ability for user to select folde

    Quote Originally Posted by BeachBum08 View Post
    1-Allow the user to select the folder from which to list all files using some sort of dialogue box
    Yes, you could use Application.FileDialog. For a routine which encapsulates this function, see http://www.cpearson.com/excel/browsefolder.aspx (Using the FileDialog). You would then call BrowseFolder from ListFilesInFolder, and pass the returned string (if it isn't vbNullString) as the first argument to ListFilesInFinanceFolder (instead of the hard-coded file path).
    2-Allow the user to select a date and only list files created after that date
    You could use the Calendar Control on a userform - see http://www.fontstuff.com/vba/vbatut07.htm. Pass the date as another parameter to ListFilesInFinanceFolder, and use If FileItem.DateCreated > theDate Then, to list files created after that date.
    Post responsibly. Search for excelforum.com

  3. #3
    Registered User
    Join Date
    07-26-2012
    Location
    Jacksonville Beach, FL
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Macro lists files in folders & subfldrs - Need to add ability for user to select folde

    Chippy -

    Thanks for the info. I took your advice for #1 and was able to get it to work perfectly!
    Now I am struggling with #2. I am using Excel 2010 and do not have the calendar form. I am trying to utilize the Date/Time picker and sort of "cheaped out" by linking the Date/Time picker value selected to a cell and then based my IF statement on the value of the cell.

    I created a new ".PDF" document and saved it to the folder whose contents I want to list on 7/27/12 to use as a test for the date selected.

    If I structure my IF statement as follows:

    Please Login or Register  to view this content.
    When I select 7/26/12 as my date and run the Macro, only files created prior to the date selected are shown.

    Just for kicks I tried to structure the statement "backwards"

    Please Login or Register  to view this content.
    When 7/26/12 is selected as my date and the Macro is run with this code, no files show up!

    What am I missing with the dates?

    I've attached an Excel workbook with the updated version of my code.

    Any help would be appreciated.

  4. #4
    Valued Forum Contributor
    Join Date
    05-21-2009
    Location
    Great Britain
    MS-Off Ver
    Excel 2003
    Posts
    550

    Re: Macro lists files in folders & subfldrs - Need to add ability for user to select folde

    Show the whole If statement:
    Please Login or Register  to view this content.
    But you said you have created a ".PDF" document, and compare the dates as shown in my first post; you don't need to use Format or Short Date because FSO file dates are the same as Excel dates - both are numbers. Therefore try:
    Please Login or Register  to view this content.
    If still no joy, try debugging the code in the VB editor.

  5. #5
    Registered User
    Join Date
    07-26-2012
    Location
    Jacksonville Beach, FL
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Macro lists files in folders & subfldrs - Need to add ability for user to select folde

    See attached workbook "FINAL".

    I named the sheet where the date is stored "Instructions" and created a named range "myDate" that includes only cell "F5". Then I adjusted my reference to the date to make it more specific.

    Please Login or Register  to view this content.
    Now it works great!

    Thanks so much for all of your help!
    Last edited by BeachBum08; 07-31-2012 at 12:18 PM. Reason: Add attachment

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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