+ Reply to Thread
Results 1 to 5 of 5

Extract specific cells from multiple excel files in same folder to new summary file

Hybrid View

  1. #1
    Registered User
    Join Date
    12-01-2014
    Location
    Brisbane
    MS-Off Ver
    2013
    Posts
    2

    Extract specific cells from multiple excel files in same folder to new summary file

    I invoice using excel and have a folder with hundreds of xls & xlsm files. I want to extract 4 specific fields ; C8;AK4; AK7 & AL11 from each file into a table a new file with NAME, DATE, INVOICE NO & AMOUNT as the headings. I'm not experienced at VBA.

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Extract specific cells from multiple excel files in same folder to new summary file

    So I created a workbook with an UPDATE tab, and a DATA tab. The Update has the button to run the macro, and the DATA tab houses the column Headers and is where the data is brought in.



    I am assuming the source sheet is Sheet1, otherwise you need to change it below.



    Sub LoopAllExcelFilesInFolder()
    'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
    '==============================================
    'Declare Variables
    '==============================================
        Dim wb As Workbook
        Dim myPath As String
        Dim myFile As String
        Dim myExtension As String
        Dim FldrPicker As FileDialog
        Dim LastRow As Long
        
        Dim NameRng As String
        Dim DateRng As String
        Dim InvoiceNumRng As String
        Dim AmountRng As String
        Dim SourceSheetName As String
    
    '==============================================
    'Optimize Macro Speed
    '==============================================
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.Calculation = xlCalculationManual
    
    '==============================================
    'Find Folder and Define Variables
    '==============================================
        NameRng = "C8"
        DateRng = "AK4"
        InvoiceNumRng = "AK7"
        AmountRng = "AL11"
        SourceSheetName = "Sheet1"
            
        Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .Title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    'In Case of Cancel
    NextCode:
        myPath = myPath
            If myPath = "" Then
            'Turn these things back on then exit
                Application.ScreenUpdating = True
                Application.EnableEvents = True
                Application.Calculation = xlCalculationAutomatic
                    MsgBox "Nothing Was Selected. Macro will End."
                   Exit Sub
            End If
      
        'Target File Extension (must include wildcard "*" to capture all files)
        myExtension = "*.xl*"
        'Target Path with Ending Extention
        myFile = Dir(myPath & myExtension)
        FileCounter = 1
    
    '===================================================================
    'Clear Data tab before begining
    '===================================================================
        ThisWorkbook.Sheets("Data").Range("A2:S1048576").Clear 'Set this range to whatever you need
            'This clears whatever range you need to
            'I assumed you want to keep Row 1 to keep the headers
        LastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 2).End(xlUp).Row 'Calculates the first Empty row on the data tab for 2nd column
        LastRow = LastRow + 1 'Adds a row so it uses the first EMPTY row
    '===================================================================
    'This isnt Necessary, only to use the statusbar to indicate how far along you are
    '===================================================================
        Do While myFile <> ""
               FileCount = FileCount + 1
                myFile = Dir()
            Loop
        myFile = Dir(myPath) ' Resets this to the first file in the folder instead of the last
    
    '==============================================
    'Loop through each Excel file in folder
    '==============================================
      Do While myFile <> ""
        If myFile <> ThisWorkbook.Name Then
            Application.StatusBar = "Importing File [" & FileCounter & " of " & FileCount & "] : " & myFile
            
            'Set variable equal to opened workbook
            Set wb = Workbooks.Open(Filename:=myPath & myFile)
            Application.Calculate
            
            'Append data from workbook
            ThisWorkbook.Worksheets("Data").Range("A" & LastRow).Value2 = wb.Sheets("Sheet1").Range(NameRng).Value2
            ThisWorkbook.Worksheets("Data").Range("B" & LastRow).Value2 = wb.Sheets("Sheet1").Range(DateRng).Value2
            ThisWorkbook.Worksheets("Data").Range("C" & LastRow).Value2 = wb.Sheets("Sheet1").Range(InvoiceNumRng).Value2
            ThisWorkbook.Worksheets("Data").Range("D" & LastRow).Value2 = wb.Sheets("Sheet1").Range(AmountRng).Value2
            
            'Recalc last row in Thisworkbook
            LastRow = ThisWorkbook.Worksheets("Data").Cells(Rows.Count, 2).End(xlUp).Row + 1
            
            'Close Workbook without Saving
            wb.Close SaveChanges:=False
        
                FileCounter = FileCounter + 1
        
        End If
        
        'Get next file name
          myFile = Dir
      Loop
    '==============================================
    'Message Box when tasks are completed
    '==============================================
      MsgBox "Imported " & FileCount & " Files Succesfully"
    
    '==============================================
    'Reset Macro Optimization Settings
    '==============================================
      Application.StatusBar = False
      Application.ScreenUpdating = True
      Application.EnableEvents = True
      Application.Calculation = xlCalculationAutomatic
      
    End Sub
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Registered User
    Join Date
    12-01-2014
    Location
    Brisbane
    MS-Off Ver
    2013
    Posts
    2

    Re: Extract specific cells from multiple excel files in same folder to new summary file

    Thanks. Sorted it out. Works great. Much appreciated. Can you tell me how to create the update tab as it would save opening vba & 'f5'ing it?

  4. #4
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Extract specific cells from multiple excel files in same folder to new summary file

    I tried attaching the workbook, but it didnt work. I woner if the 2.5MB size is a problem.

    Either way, just create a DATA dab and include column headers in A1:D1

  5. #5
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Extract specific cells from multiple excel files in same folder to new summary file

    Google is your friend.

    I'm on my phone or I'd send you a link. Look up how to add macro button (with the developer tab). You add the button then link it to the macro. You can have the button on the DATA tab if you want, it doesnt matter.

+ 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: 7
    Last Post: 09-29-2014, 09:24 AM
  2. Merging cells from multiple files in a folder into one file
    By Alteregoist in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2013, 04:26 AM
  3. [SOLVED] How to Extract Data from Multiple Files in Same Folder and Copy Data to One File
    By champ052005 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-10-2013, 01:25 PM
  4. Extract specific column data from specific sheet from multiple files in a folder
    By piggyfox in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-31-2013, 11:51 AM
  5. Replies: 1
    Last Post: 05-24-2007, 07:28 AM

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