+ Reply to Thread
Results 1 to 12 of 12

Macro to copy file names and data from each file in a folder into master spreadsheet

  1. #1
    Registered User
    Join Date
    06-29-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    7

    Macro to copy file names and data from each file in a folder into master spreadsheet

    Hi everyone,

    I'm new to this forum as I only found it a week or so ago when looking for some help with a macro problem. I found some great help with my previous problem so I was hoping someone could help me with another issue I'm having. I've had a look at old threads and have found some code but I can't seem to edit and combine it correctly to fit exactly what I want.

    Basically I have a number of excel files in a folder. I want to create a macro that will copy the name of each file (preferably without the .xls but that's not hugely important) and some data from each into a master spreadsheet. Each file has three columns of data - A, B and C. I only want to copy column C for each (this column does contain some blank cells which I don't want copied).

    So in my master spreadsheet I want column A to contain the name of the first file in the folder and the data from column C. Column B should contain the name of the second file in the folder and the data from column C. And so on.

    So far I've found two pieces of code - one that lists the file names and one that copies the data but they aren't working how I would like and I need to combine them into one macro if possible.

    The first:

    Please Login or Register  to view this content.
    The problem with this is that it pastes the file names vertically in column A starting from cell 2. I need the names to go across row 1 starting from column A (like a header row).


    The second:

    Please Login or Register  to view this content.
    The problem with this is that it copies the data in columns A, B and C from each file when I only want column C. Also it pastes the data vertically whereas I require it to be horizontally.
    In addition, the data does not seem to have been pasted directly. Instead of having the data form the first file, then the data from the next file etc it seems to have grouped together data which is the same across multiple files. So for example each file might contain a cell in a column A with the text "ff_add_paragraph". After pasting the data into the master file the first 100 or so cells will be "ff_add_paragraph" because its copied that value from 100 files and pasted them all in a row instead of pasting everything directly. I also noticed that it doesn't seem to have pasted everything from each file as when I checked one of the files there were some values which did not show up at all in the master file.

    I hope I've explained this properly. As you can probably tell I have absolutely no experience with Macros or VBA so I would really appreciate it anyone could help me out! If you need any other information just let me know!

    Thanks!
    Last edited by arlu1201; 07-09-2012 at 07:43 AM. Reason: Use code tags in future.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    Do you have a sample file that you can upload?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    06-29-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    No problem.
    Sample 1 and 2 are files which show the type of data I have in each of the files to be copied. Column C is the column that I need to copy into the mastersheet along with the file name.

    Thanks very much!
    Attached Files Attached Files

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    Try this code
    Please Login or Register  to view this content.
    It clears the contents of the masterfile each time you run it. So only fresh data is included in the master file. If you do not want this to happen, remove this line of code
    Please Login or Register  to view this content.
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose Tools | Macro | Macros
    Select a macro in the list, and click the Run button

  5. #5
    Registered User
    Join Date
    06-29-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    Hi,

    Thanks very much for your code. I'm getting an error because the worksheet name in each of the files is actually the file name (minus the .xls) instead of Sheet1. These excel files were all previously converted from text files which I assume is the reason for this. How would I go about editing the code above so the line:

    Set sourceData = sourceBook.Worksheets("Sheet1")

    says the worksheet name instead of Sheet1?

    Thanks again!

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    Try this...change that line from
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-29-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    Thanks so much, that fixed it!
    One last issue, sorry I should have mentioned this in the beginning but I forgot about it - the cells in column C which I'm copying from each file contain some formulas. So I need it to paste special into the masterfile. Is there a line I can alter to do this?

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    Change this line from
    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    06-29-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    Perfect, thank you!
    Hopefully this will be the last issue - and maybe it's due to the files being copied rather than the macro - but it seems to be copying a number of blank cells. For example in the column in the master file cell 1 contains the file name, cells 2-104 are blank and the data values begin at cell 105. Similar for the other columns. Is it possible to edit it so that blank values are ignored?

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    The code works in this way -

    It copies the filename in row 1. Then copies the data from the source file as is. Then it sorts column 1 so that the blanks are removed. Then it moves to column 2.

    So the blanks shouldnt be there.

    Are those really blanks or is there some formula in them?

  11. #11
    Registered User
    Join Date
    06-29-2012
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    They are actually formulas in the source data, you're right. They just return a blank value. So I assume then there's no way to exclude them from the paste?

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro to copy file names and data from each file in a folder into master spreadsheet

    We can do this.

    Once the data is copied, we can run a small bit of code to remove the blank rows.

+ 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