+ Reply to Thread
Results 1 to 13 of 13

macro: paste cell from specific files to 1 general file

Hybrid View

  1. #1
    Registered User
    Join Date
    03-10-2009
    Location
    London, Ontario
    MS-Off Ver
    Excel 2007
    Posts
    18

    Question macro: paste cell from specific files to 1 general file

    hi there,

    i have about 80 files that all have the same structure. I also have a general excel file that I need to fill with values from each of the 80 files. I'd like to use a macro that can be applied to each of the 80 files at a time (or at the same time, if that's possible) so I can copy the cells (only the text, not the formula) I need from each individual file to the general sheet.

    e.g.,
    each file is:

    xyz 123
    abc 456
    ghi 956

    I need to copy the "123" to a cell in the general sheet, "456" to a different cell in the general sheet and so on (to be able to choose which cell in the general sheet I am copying to).

    Thank you
    Last edited by jy2009; 08-27-2009 at 11:42 PM.

  2. #2
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: macro: paste cell from specific files to 1 general file

    HI
    It is possible to pull data from multiple files to a single file. Pl give me more details of which cells you want pulled (from which sheet (name)) and where do they go in general book
    Ravi

  3. #3
    Registered User
    Join Date
    03-10-2009
    Location
    London, Ontario
    MS-Off Ver
    Excel 2007
    Posts
    18

    Question Re: macro: paste cell from specific files to 1 general file

    Someone provided me with a code that almost works (problem is that it copies and pastes randomly):

    " requires that you copied all 80+ files you want to extract data from in one folder (you need to provide the path name for the constant SOURCE_FOLDER). The macro opens each of the workbooks in that folder, extract the data from cell B1 and copy it to the next empty cell in column A of the active worksheet in the workbook you are running the macro from."

    Option Explicit
    Const SOURCE_FOLDER = "C:\..."
    Sub AggregateDataFromFiles()
    Dim fs As Object
    Dim objFolder As Object
    Dim objFolderName As String
    objFolderName = SOURCE_FOLDER
    Dim filePath As String
    Dim objFile As Object

    Dim targetWb As Workbook
    Dim lastrow As Long
    lastrow = 1
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fs.GetFolder(objFolderName)
    For Each objFile In objFolder.Files
    filePath = objFolderName & "\" & objFile.Name
    Set targetWb = GetObject(filePath)
    targetWb.Worksheets(1).Range("B1").Copy Destination:=ActiveSheet.Range("A" & lastrow)
    lastrow = lastrow + 1
    targetWb.Close (False)
    Next
    End Sub
    However, the copying and pasting is happening randomly into the summary sheet. But, I'd like that it grab the cell and paste it in the same order every time (from the first file in the folder, follower by the second etc.). Right now it's picking a random file in the folder that's specified and moving it into the column that's specified. Is there a way for it to copy first from the first file in the folder (the one at the very top of the folder window) followed by those below?

  4. #4
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: macro: paste cell from specific files to 1 general file

    HI
    Save the attached file inside the folder containing 80+ files. Assuming sheet name is sheet1, run the macro in the attached file. It lists files in col A and B1value in col B. How do you want the files arranged (what is first and second in your case)
    Ravi
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-10-2009
    Location
    London, Ontario
    MS-Off Ver
    Excel 2007
    Posts
    18

    Question Re: macro: paste cell from specific files to 1 general file

    Hi Ravi,


    That's great! Thank you

    right now it lists the name of the file in column A (which is great because it tells me what the value is about) and the column B values are all 0.

    Column B values should be equal to Column AP, cell 100 in Sheet 1.
    Column C Column AP, cell 101
    Column D AP 102

    E AP 104
    F AP 105
    G AP 106

    H AP 108
    I AP 109
    J AP 110

    K AP 112
    L AP 113
    M AP 114

    N AT 48


    Would that be doable?

    Thank you so much for your help.

  6. #6
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: macro: paste cell from specific files to 1 general file

    Hi
    Try the modified codes
    Ravi
    Attached Files Attached Files

+ 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