+ Reply to Thread
Results 1 to 8 of 8

Macro to find most recent csv file in folder and import this

  1. #1
    Registered User
    Join Date
    11-25-2013
    Location
    Romania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Macro to find most recent csv file in folder and import this

    Hi, I'm new to excel and VBA but I have a problem at work you do not know how to fix it.
    In one folder we have several files titled as below

    CoduriEAN01-Jun-15.csv
    CoduriEAN02-Jun-15.csv
    CoduriEAN04-Mai-15.csv
    CoduriEAN06-Jun-15.csv

    I would like to run a macro to check that the files have more recent date in name and to import In one new tab.
    Code must check only files that have the name "CoduriEAN**" because in that folder are other .csv file with another name but those they date in name.

    I enclose a csv file to see what has to be imported.
    Thanks in advance for any ideas and apologize if I did not write properly in English.
    Attached Files Attached Files

  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,259

    Re: Macro to find most recent csv file in folder and import this

    Hello electros,

    Welcome to the Forum!

    How would you tell which of these 3 files is most recent?

    CoduriEAN01-Jun-15.csv
    CoduriEAN02-Jun-15.csv
    CoduriEAN06-Jun-15.csv
    NOTE: Never mind, I see it now. Need more coffee!
    Last edited by Leith Ross; 06-06-2015 at 04:04 PM.
    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!)

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

    Re: Macro to find most recent csv file in folder and import this

    Hello

    Try this macro out. Hopefully VBA will not have problems with the dates. Change the folder path in the macro to match where your files are.

    This macro will scan the folder for most recent csv file starting with "CoduriEAN". The file date is extracted from the file name and the most recent date and file name are saved. Once all files have be checked, the most recent file will be opened in Excel.
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 06-06-2015 at 05:28 PM.

  4. #4
    Registered User
    Join Date
    11-25-2013
    Location
    Romania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to find most recent csv file in folder and import this

    Thanks for the reply but the code does not work posted.
    I give me the error in the first screenshot, and when I hit debug are posted on the yellow line in the screenshot two
    Please help me
    Attached Images Attached Images

  5. #5
    Registered User
    Join Date
    11-25-2013
    Location
    Romania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to find most recent csv file in folder and import this

    I resolved the error, I modified this line of code

    Please Login or Register  to view this content.
    as shown below

    Please Login or Register  to view this content.
    and now works as you said, but not as I would like. I do not want to open csv file again In one document but I want it to be imported into the active document in an existing sheet called "CoduriEAN" for example

    Please help

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

    Re: Macro to find most recent csv file in folder and import this

    Hello electros,

    This macro will work with data based on the examples you posted. If your actual data is different then I can not guarantee it will work as intended.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    11-25-2013
    Location
    Romania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to find most recent csv file in folder and import this

    Hello Leith Ross
    Thanks for your answer but posted code does not work for me.
    I give me the error in the image below and do not know what to them.
    Attached Images Attached Images

  8. #8
    Registered User
    Join Date
    11-25-2013
    Location
    Romania
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to find most recent csv file in folder and import this

    I solved the above error,was the regional settings. I put the English(US) and now works but if in this directory is a file from 2014 (for example CoduriEAN31-Mar-14.csv).It can be set to search only the current year?
    P. S.
    I have tested and found that this code only check day which is higher and picks it.Do not check the month and year.This code is not correct for me. It should check all files of this year and this month and to choose the most new. This folder contains files from 2013 and 2014 and of course the 2015 that I am not allowed to delete them.
    Last edited by electros; 06-08-2015 at 05:55 AM.

+ 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. Open a file in the most recent folder
    By primaldeity in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-27-2013, 07:34 PM
  2. Macro For opening Most recent file in folder
    By dtaganovic16 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-13-2012, 12:00 PM
  3. [SOLVED] Macro to import most recent File
    By jferris266 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-07-2012, 05:29 PM
  4. Find most recent file in recursive filtered folder
    By camaytoc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-06-2011, 05:58 PM
  5. Selecting the most recent file in a folder by Macro/VBA script
    By scott.k@ems-t.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2006, 01:15 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