+ Reply to Thread
Results 1 to 12 of 12

Function to return last modified file

  1. #1
    Registered User
    Join Date
    12-05-2008
    Location
    USA
    Posts
    6

    Function to return last modified file

    First of all, I don't really know VBA more than just piecing together other people's work (so please dumb down your responses as much as possible).

    So I would like to have a function that displays a filename within a cell. The filename I need to display will start with "cost" and have a date at the end (not always the same number of characters). I need to search a folder for all files that contain that text and return the last modified one.

    I found this VBA code that returns the last modified file, but only filters based on the extension (which I would like to keep in the code as well).

    Please Login or Register  to view this content.
    I found a code that would filter the files but don't know how or where exactly to put it in:
    Please Login or Register  to view this content.
    Any help would be greatly appreciated.


    EDIT: By the way, I would like to have the part of the filename used to filter be taken from a cell. In other words, if the beginning of the filename I am searching for changes, I would like for users to be able to update the search criteria in a cell rather than having to open the VBA code. This isn't necessary but would be a plus.
    Last edited by dissonanceUS; 01-05-2009 at 05:56 PM.

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

    Welcome to the Forum!

    Will the files always be located in the same folder?

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    12-11-2008
    Location
    Lexington, KY
    Posts
    14
    Initially, I'm thinking a good solution may be to load the filtered file names and 'Modified Dates' into an array, then pass each array item in turn into a variable that will retain the most recent 'Modified Date'. All you have to do then is return the variable to the cell. Does this sound viable? ~R

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387
    Last edited by stanleydgromjr; 12-22-2008 at 08:16 PM.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  5. #5
    Registered User
    Join Date
    12-05-2008
    Location
    USA
    Posts
    6
    Quote Originally Posted by Leith Ross View Post
    Hello dissonanceUS,

    Welcome to the Forum!

    Will the files always be located in the same folder?

    Sincerely,
    Leith Ross
    The file with the VBA code will be one place but the files its searching through will all be another place together (in the same folder)

  6. #6
    Registered User
    Join Date
    12-05-2008
    Location
    USA
    Posts
    6
    Quote Originally Posted by GOOS View Post
    Initially, I'm thinking a good solution may be to load the filtered file names and 'Modified Dates' into an array, then pass each array item in turn into a variable that will retain the most recent 'Modified Date'. All you have to do then is return the variable to the cell. Does this sound viable? ~R
    I don't know, I don't really know what I'm doing here. My VBA skills really just includes hiding stuff and other simple tasks. This one is a bit out of my league.

  7. #7
    Registered User
    Join Date
    12-05-2008
    Location
    USA
    Posts
    6
    Thanks, I'm trying to play with one of those, I'll post if I can figure out how to make it do what I'm trying.

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

    After looking at the other examples you posted, I hope you can clear up a few questions I have, so I can write something that will work for you.

    1. The cell you are referring to will contain the file to be search for, yes?
    2. Can you provide an example of one or more file names?
    3. Will these files all have the same extension or do you want to match only the file name?
    4. Do you want to select the folder using a file dialog or have it hard coded into the macro?
    5. Where do you want the results to be logged, on another worksheet or somewhere else?

    Sincerely,
    Leith Ross

  9. #9
    Registered User
    Join Date
    12-05-2008
    Location
    USA
    Posts
    6
    Quote Originally Posted by Leith Ross View Post
    Hello dissonanceUS,

    After looking at the other examples you posted, I hope you can clear up a few questions I have, so I can write something that will work for you.

    1. The cell you are referring to will contain the file to be search for, yes?
    2. Can you provide an example of one or more file names?
    It will contain the part of the filename that will be used to search, yes.

    For example, the cell being referenced will contain "cost" and will be searching for one of the following files:
    cost_12dec2008.xls
    cost_13april2008.xls
    cost_8may2008.xls
    cost_6may2008.xls

    Quote Originally Posted by Leith Ross View Post
    3. Will these files all have the same extension or do you want to match only the file name?
    All of the files I care about will be Excel files (.xls) but the folders will probably have other files in it with similar names so it would be nice to have it filter out extensions as well.

    Quote Originally Posted by Leith Ross View Post
    4. Do you want to select the folder using a file dialog or have it hard coded into the macro?
    I would like for the directory path to be referenced from a cell. So if the directory changes, which usually happens at least yearly, it would be simple for any user to correct.

    Quote Originally Posted by Leith Ross View Post
    5. Where do you want the results to be logged, on another worksheet or somewhere else?

    Sincerely,
    Leith Ross
    I don't need a log output, just the single file name to be inserted into a cell (cell E2 in the example screenshot). I already have a macro that uses a filename contained in a cell and a directory path in another cell to fill in the rest of the worksheet by drawing the link from those 2 cells.

    An example of what the top of the worksheet looks like is attached as "vba-cost.jpg"

    The code I am using to fill in the worksheet is like this:
    Please Login or Register  to view this content.
    Attached Images Attached Images
    Last edited by dissonanceUS; 12-23-2008 at 03:12 PM.

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

    Thanks for answering my questions. Now I know what you want do. I will write a macro based on your screen shot.

    Sincerely,
    Leith Ross

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

    This macro will find all files that begin with the name in cell "E1" that have ".xls" extensions. The name of the file that was modified last will be placed in cell "E2". The folder path in cell "E3" is used to search for these files. The macro is written to use the Active Sheet. You can change the code to look at a specific worksheet for this file information. For example: Change "ActiveSheet" to Worksheets("Sheet1"). The worksheet name is in red.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  12. #12
    Registered User
    Join Date
    12-05-2008
    Location
    USA
    Posts
    6
    Code works great...

    Thanks for the help.

+ 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