+ Reply to Thread
Results 1 to 2 of 2

Pulling data from 3 different files into a master file

Hybrid View

  1. #1
    Registered User
    Join Date
    03-02-2017
    Location
    India
    MS-Off Ver
    2016
    Posts
    24

    Pulling data from 3 different files into a master file

    Hi, So I am trying to pull data from 3 different files into one master file. All the 3 files have the same amount of columns but the rows keep increasing or decreasing.
    I am also trying to stack the data one below another. So I pull all the columns from Book1 which includes the headers too, after that headers are not needed so I delete the headers from the other 2 files while doing so..

    I have successfully pulled data from Book1, but I have gotten stuck on Book2 hence can't finish the process..
    Here is the code so far..

    Sub pullingData()
        
        Dim filePath As String
        Dim SourceWb As Workbook
        Dim TargetWb As Workbook
        
        
        'Pulling from book1
        
        
        Set TargetWb = ActiveWorkbook
        filePath = TargetWb.Sheets("Master").Range("A1").Value
        Set SourceWb = Workbooks.Open("C:\Users\xyz\Desktop\Book1.xlsx")
    
        For i = 1 To 3
            SourceWb.Sheets("Sheet1").Range("A:J").Copy Destination:=TargetWb.Sheets("Master").Range("A1")
        Next i
    
        SourceWb.Close savechanges:=False
        
        Range("A" & Rows.Count).End(xlUp).Offset(1).Select
        
        
        'Pulling from book2
        
        Set TargetWb = ActiveWorkbook
        
        filePath = TargetWb.Sheets("Master").Range("A" & Rows.Count).End(xlUp).Offset(1).Value
        Set SourceWb = Workbooks.Open("C:\Users\xyz\Desktop\Book2.xlsx")
        Range("1:1").Select
        Selection.Delete
    
        For i = 1 To 3
            SourceWb.Sheets("Sheet1").Range("A1" & Selection.End(xlToRight) & Selection.End(xlDown)).Copy Destination:=TargetWb.Sheets("Master").Range("A" & Rows.Count).End(xlUp).Offset(1)
        Next i
    
        SourceWb.Close savechanges:=False
        
        
        
    End Sub
    Attached Files Attached Files
    Last edited by AnirudhC; 03-31-2017 at 04:50 PM.

  2. #2
    Valued Forum Contributor spitfireblue's Avatar
    Join Date
    01-29-2015
    Location
    Adelaide, Australia
    MS-Off Ver
    2007,2010,2016
    Posts
    611

    Re: Pulling data from 3 different files into a master file

    Put the files in a single folder and then try this (change myPath to suit):

    Sub CopyDataFromFiles()
    
    Dim wbM, wb As Workbook
    Dim myPath, myFile, myExt As String
    Dim nr, lr As String
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    
    Set wbM = ThisWorkbook
    myPath = "C:\temp\Data Files\"
    myExt = "*.xls*"
    myFile = Dir(myPath & myExtension)
    
    Do While myFile <> ""
        Set wb = Workbooks.Open(Filename:=myPath & myFile)
        lr = wb.Sheets(1).Range("A" & wb.Sheets(1).Rows.Count).End(xlUp).Row
        nr = wbM.Sheets(1).Range("A" & wbM.Sheets(1).Rows.Count).End(xlUp).Row + 1
        wb.Sheets(1).Range("A2:J" & lr).Copy Destination:=wbM.Sheets(1).Range("A" & nr)
        wb.Close SaveChanges:=False
        myFile = Dir
    Loop
    
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
    End Sub
    Regards,
    Stephen

    If you feel someone has helped you please thank them and click on the star on their post to add reputation.
    Please ensure that you use code tags where relevant, and mark solved threads as solved.
    Most of all please be respectful and understanding of others.

+ 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. Replies: 4
    Last Post: 12-04-2014, 12:14 PM
  2. Help with data import from files to master file
    By RoyLittle0 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-04-2013, 03:46 PM
  3. Consolidate Data from Multiple Files (Different File Names) into a Master File
    By dspraveen_23 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-08-2012, 12:59 PM
  4. Pulling data from multiple files into holding file
    By Darkfish in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2009, 10:13 AM
  5. Pulling data from files by part of file name
    By James580 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-06-2008, 04:35 PM
  6. compiling/pulling data (range) in different files into one file
    By perinouk in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-27-2007, 08:28 AM
  7. Pulling data from individual files to master list
    By joshwimmer@gmail.com in forum Excel General
    Replies: 1
    Last Post: 05-22-2006, 05:40 PM

Tags for this Thread

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