+ Reply to Thread
Results 1 to 13 of 13

Excel Automation For reading data from .csv files in a folder

  1. #1
    Registered User
    Join Date
    05-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Exclamation Excel Automation For reading data from .csv files in a folder

    Here is what I want to accomplish. I have no coding experience and would like someone to help me in completing this task, please !

    Everyday (at 0800/1200/1700 Hrs) I receive reports in .csv format and when I open the files in Excel, they are neatly formatted with the data but the data is huge. The information I wanted is under E2:F14 range. Also the file I receive will have the name in the format: current_201106070800.csv (current_YYYYMMDDHHHH.csv). I have these files for the past 6 months so mining the data is not a option for me.

    Now, I would like to gather the information from all the reports I receive and house in a master excel sheet (A Master Data Template) with a macro which can:

    - Scan the folder for new files and if there are any new files found grab the information (from the range E2:F14 range) from the new file and copy to the master excel file. (This is where I need help! )

    I'm looking for a output in the master excel sheet something like this:

    -----A------------B--------------------------C
    1---Period------201106070800-------201106071200
    2---Data1-------7890--------------------89098
    3---Data 2------890878-----------------94580
    4---Data 3------898090-----------------902850928

    So, B1 is the file name from where the data is extracted, B2:B4 is the data copied from the file to master excel sheet. Same goes for C1:C4

    I can manually copy the files everyday from my email to the folder and open the master excel sheet and run the macro to receive the data.

    The next tasks I will be able to complete by myself, to refine the data further and generate pivot tables/reports and stuff.

    Can someone please share the code if already available. I would appreciate any help in complete the macro task.

    Thanks in advance. Have a nice day.
    Last edited by nalayak21; 06-08-2011 at 10:03 AM.

  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

    Re: Excel Automation For reading data from .csv files in a folder

    Hello nalayak21,

    Welcome to the Forum!

    It would be very helpful to see a copy of the csv file. You should zip the file before you post it.

    To Attach a File:
    1. Scroll down to the window below your post Additional Options
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.
    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
    Registered User
    Join Date
    05-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excel Automation For reading data from .csv files in a folder

    Thanks Leith! I have attached a sample file.
    Attached Files Attached Files

  4. #4
    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: Excel Automation For reading data from .csv files in a folder

    Hello nalayak21,

    Thanks for the sample file. Are the files laid out the same using just columns "A" and "B"?

  5. #5
    Registered User
    Join Date
    05-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excel Automation For reading data from .csv files in a folder

    Quote Originally Posted by Leith Ross View Post
    Hello nalayak21,

    Thanks for the sample file. Are the files laid out the same using just columns "A" and "B"?
    Leith:

    I have modified the original file and made a sample out of the original to post here. Did not want to expose the data here in the forum

    Yes, I just need the data from two columns. Precisely from column E and F and the data range is E2:F14

    Thanks a lot !

  6. #6
    Registered User
    Join Date
    05-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Thumbs up Re: Excel Automation For reading data from .csv files in a folder

    Here is the sample Summary Template file. I have put some comments for the data and the column names.

    Thanks
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    05-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excel Automation For reading data from .csv files in a folder

    Help please !

  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

    Re: Excel Automation For reading data from .csv files in a folder

    Hello nalayak21,

    Here is the macro. You will need to change the folder where the files are stored. It is marked in bold text. Each file will be added the "Master" sheet starting with the next empty column to the right. Borders are added around each group of cells after they are copied over. The file is chosen by the latest modification date. This ensures the most up to date is used.
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    05-18-2011
    Location
    Delhi , India
    MS-Off Ver
    Excel 2007
    Posts
    171

    Re: Excel Automation For reading data from .csv files in a folder

    Hello Leith Ross ,

    i have kind of a same query in which i download a csv file in the desktop and then i have to copy the whole worksheet from that file to another workbook. can it be done automatically from the desktop . both the files (csv file and the workbook named Book1) are on the desktop only.

    thanks

  10. #10
    Registered User
    Join Date
    05-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Thumbs up Re: Excel Automation For reading data from .csv files in a folder

    Leith:

    Thanks a ton for your help on this. I do see a problem when I ran the code. I get the "400" error with no further information.

    I guess I see the issue. If you look at the files (attached now with this message) if you see the data file and open in notepad, I see the formulas and I guess that where the code is failing. Is that true? The attached are the true data file and the template I'm working on. I just masked some info.

    Could you please see where I'm doing wrong.

    Appreciate your help, Leith.


    Quote Originally Posted by Leith Ross View Post
    Hello nalayak21,

    Here is the macro. You will need to change the folder where the files are stored. It is marked in bold text. Each file will be added the "Master" sheet starting with the next empty column to the right. Borders are added around each group of cells after they are copied over. The file is chosen by the latest modification date. This ensures the most up to date is used.
    Attached Files Attached Files

  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

    Re: Excel Automation For reading data from .csv files in a folder

    Hello nalayak21,

    I downloaded the files, changed the directory path in the macro to point to files, and it ran with no errors. Unfortunately, error 400 in Windows is a general error. To get any meaningful information, you will need to debug the macro by stepping through it line by line. Once you have located the problem line of code, I can help you fix it.

  12. #12
    Registered User
    Join Date
    05-08-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Excel Automation For reading data from .csv files in a folder

    Leith:

    My apologies. I ran the test on a desktop with Excel 2003 and the code worked with charm! However, when I try to run on my laptop with Excel 2007 it never works. Just gives a 400 Error. I F8'ed the code and here is where a error is thrown:

    Please Login or Register  to view this content.
    Runtime Error: 1004
    Application defined or object defined error.

    I tried to add Trusted Location and change Macro Security settings on Excel 2007 but no results.

    Also, when I run on Excel 2003 there is a strange behavior I observed.

    When I place a file in data folder and run the code it runs and even for the second file. But for the third file when I try to run the macro the "Run" button is greyed out. I close the file and open again and I can run again. However, I can run the code from the VB window with no issues.

  13. #13
    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: Excel Automation For reading data from .csv files in a folder

    Hello nlayak21,

    Let's formalize the Open statement more instead of relying on defaults.

    Change the CSVwkb open statement to this...
    Please Login or Register  to view this content.

+ 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