+ Reply to Thread
Results 1 to 14 of 14

Selecting files based on date range

  1. #1
    Registered User
    Join Date
    01-17-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    14

    Selecting files based on date range

    Hi all -

    I have a code which will copy the first page of all Excel files within a specific directory to a new workbook then do a search for a keyword and return the results. The problem is the the files in the source directory grow every day. I need to incorporate some sort of date range function which will let me choose files created between specific dates. Do you have any suggestions/knowledge/code that you think would help?

    Thanks
    FIS Mitch

  2. #2
    Forum Contributor
    Join Date
    05-08-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    102

    Re: Selecting files based on date range

    FISMitch,

    Consider using the FileDateTime function. This function returns the date and time that a file was created or modified. Here is a link that describes how to use the function: http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

    What you can do is write an if-then statement into your current procedure that uses the output of the FileDateTime fuction for each Excel file in your directory. If the date returned is between the two dates you specify, then proceed with the copy, paste, and search actions. If it is not in between these two dates, then do nothing.
    ______________________
    [1] If this reply was helpful, click the star to the left.
    [2] If this reply answered your question, mark this thread as [SOLVED].

  3. #3
    Registered User
    Join Date
    01-17-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Selecting files based on date range

    Thanks Dimitriov -

    I attempted to incorporate the FileDateTime function into my code but I am getting a Run Time Error 9 on the If line. Any assistance would be much appreciated.

    Thanks again.

    Please Login or Register  to view this content.
    Moderator Note:

    Pls use code tags around your code next time as per forum rules.
    Last edited by Fotis1991; 09-24-2013 at 12:28 PM.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Selecting files based on date range

    I can't test this but I've made a couple of changes to your code which should bring you closer to working code:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Registered User
    Join Date
    01-17-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Selecting files based on date range

    Thank you xladept -

    Your changed did get me further but I am finding an issue where S and E are showing = Empty. When this happens the code makes a copy of the first sheet of the workbook I have this code in. I attempted a couple different date formats to see if that might be the issue but did not have any luck.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Selecting files based on date range

    Oh it's varS ad varE - sorry

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-17-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Selecting files based on date range

    Thanks again xladept -

    The variables are now working but the code is making copies of the first sheet of the workbook the code is in. Any addiitional suggestions?

    Thanks again -
    FISMitch

  8. #8
    Forum Contributor
    Join Date
    05-08-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    102

    Re: Selecting files based on date range

    FISMitch,

    I suspect that is happening because the workbook that contains the code is in the same directory as the files the code is operating on. Would you be okay with keeping "ThisWorkbook" in a different folder? From the looks of it, this should be fine, since your code above spells out the directory that VBA should look into, i.e., H:\BSD MM Search\Test.

    Hope this helps.
    __________________________
    [1] If this reply was helpful, click the star to the left.
    [2] If this reply answered your question, mark this thread as [SOLVED].

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Selecting files based on date range

    Hello FISMitch,

    Give this a try and let me know how it works out.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Selecting files based on date range

    I moved the "End IF"

    Please Login or Register  to view this content.
    Oh - it's the same thing Leith has done
    Last edited by xladept; 09-24-2013 at 07:52 PM.

  11. #11
    Registered User
    Join Date
    01-17-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Selecting files based on date range

    xladept and Leith -

    I truly appreciate your assistance with my questions. The code modifications have stopped recreating the first worksheet of the workbook the code is, however it is not opening/closing/copying from the workbooks that fall into the date range I have specified in A1 (my start date) and B1 (my end date). I can only think it is the date format I am entering. I checked the regional settings and the date timestamp for the files I need to import and tried several different formats but that did not help. Do you know what date format I need to use in cells A1 and B1 that act as my range. Also, I noticed when I placed a date in cell B1 that was outside of the date range of files that are in the \test directory I got an error Run time error 438 on the oFile.Open line.

    Thanks again -

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Selecting files based on date range

    It's working for me?

    Please Login or Register  to view this content.
    BTW - Why is oFso being created??
    Last edited by xladept; 09-26-2013 at 04:59 PM.

  13. #13
    Registered User
    Join Date
    01-17-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Selecting files based on date range

    Quote Originally Posted by xladept View Post
    It's working for me?

    Please Login or Register  to view this content.
    BTW - Why is oFso being created??

    Thanks again xladept. I have tried and it is still not working properly. What is the date format you are entering in A1 and B1?

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Selecting files based on date range

    Hi,

    These are my dates in A1 and B1: 6/1/2011 9/25/2013

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Selecting data from a date range based on the current date
    By ian-sheridan in forum Excel General
    Replies: 12
    Last Post: 06-20-2013, 11:38 AM
  2. Replies: 12
    Last Post: 03-07-2013, 11:02 PM
  3. Replies: 0
    Last Post: 03-30-2012, 01:56 PM
  4. Selecting and Copying a Cell or a Range of Cells from One or More Files or Worksheets
    By Tyler Peco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-27-2008, 03:55 PM
  5. selecting date range / data range autofilter
    By hansiman in forum Excel General
    Replies: 1
    Last Post: 02-23-2005, 09:06 PM

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