Macro to Extract Specific cell data from Multiple Worksbooks into one summary.

Dreamwrx

New Member
Joined
Mar 17, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a bunch of excel files (3000) that I need to extract data from specific Cells in these spreadsheet and paste them to a summary sheet.

Q10:V10 -> B8:G8;
Q13:V13 -> H8:M8;
Q16:V16 -> N8-S8;
B14 -> A8

Next Spreadsheet would be
Q10:V10 -> B9:G9;
Q13:V13 -> H9:M9;
Q16:V16 -> N9:S9;
B14 -> A8

so on and so forth.

I have been reading multiple older forum posts and have tried some things but as I am not proficient at VB.. I seem to be stuck.

The following was what I was using. Most of it was from Jerry Beaucaire's code

VBA Code:
Sub Retrieve_Data()
Dim fName As String, fPath As String, fPathDone As String
Dim LR As Long, NR As Long
Dim wbData As Workbook, wsMaster As Worksheet

'Setup
    Application.ScreenUpdating = False  'speed up macro execution
    Application.EnableEvents = False    'turn off other macros for now
    Application.DisplayAlerts = False   'turn off system messages for now
   
    Set wsMaster = ThisWorkbook.Sheets("AVG")    'sheet report is built into

With wsMaster
    If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
        .UsedRange.Offset(1).EntireRow.Clear
        NR = 2
    Else
        NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1    'appends data to existing data
    End If

'Path and filename (edit this section to suit)
   ' MsgBox "Please select a folder with files to consolidate"
   ' Do
   '     With Application.FileDialog(msoFileDialogFolderPicker)
   '         .InitialFileName = "C:\2010\Test\"
   '         .AllowMultiSelect = False
   '         .Show
   '         If .SelectedItems.Count > 0 Then
   '             fPath = .SelectedItems(1) & "\"
   '             Exit Do
   '         Else
   '             If MsgBox("No folder chose, do you wish to abort?", _
   '                 vbYesNo) = vbYes Then Exit Sub
   '         End If
   '     End With
   ' Loop
   fPath = "C:\Users\htn\Desktop\Macro_Test\"                  'remember final \ in this string
    fPathDone = fPath & "Imported\"     'remember final \ in this string
    On Error Resume Next
        MkDir fPathDone                 'creates the completed folder if missing
    On Error GoTo 0
    fName = Dir(fPath & "*.xls*")       'listing of desired files, edit filter as desired

'Import a sheet from found files
    Do While Len(fName) > 0
        If fName <> ThisWorkbook.Name Then              'don't reopen this file accidentally
            Set wbData = Workbooks.Open(fPath & fName)  'Open file
        'This is the section to customize, replace with your own action code as needed
            Dim ws As Worksheet
            For Each ws In wbData.Sheets(Array("Flow_Level"))
                LR = Range("B" & Rows.Count).End(xlUp).Row  'how many rows of info?
                If LR > 3 Then
                    wsMaster.Range("A" & NR) = Replace(Range("A1"), "Group ", "")
                        Range("Q10:V10").Copy
                    wsMaster.Range("B8" & NR).PasteSpecial xlPasteValues, Transpose:=True
                    wbData.Close False                                'close file
                    NR = wsMaster.Range("B" & Rows.Count).End(xlUp).Row + 1
                End If
   
   
    Next ws
        ' wbData.Close False   'close data workbook
        ' fName = Dir         'get the next filename
        Name fPath & fName As fPathDone & fName           'move file to IMPORTED folder
        End If
        fName = Dir                                       'ready next filename
    Loop
   
    Range("A3:A" & NR - 1).SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    With Range("A3:A" & NR - 1)
    .Value = .Value
    End With
End With
ErrorExit:    'Cleanup
    ActiveSheet.Columns.AutoFit
    Application.DisplayAlerts = True         'turn system alerts back on
    Application.EnableEvents = True          'turn other macros back on
    Application.ScreenUpdating = True        'refreshes the screen
End Sub

Thank you for any help in advance.
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to MrExcel forums.

Try this macro, changing the workbooks folder and filespec and destination sheet name where indicated.
VBA Code:
Public Sub Copy_Values_From_Workbooks()

    Dim matchWorkbooks As String
    Dim destSheet As Worksheet, r As Long
    Dim folderPath As String
    Dim wbFileName As String
    Dim fromWorkbook As Workbook
   
    'Folder path and wildcard workbook files to import cells from
   
    matchWorkbooks = "C:\folder\path\*.xls"                                             'CHANGE THIS
   
    'Define destination sheet
   
    Set destSheet = ActiveWorkbook.Worksheets("Summary")                                'CHANGE THIS
   
    destSheet.Cells.Clear
    r = 0
   
    Application.ScreenUpdating = False
           
    folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
    wbFileName = Dir(matchWorkbooks)
    While wbFileName <> vbNullString
        Set fromWorkbook = Workbooks.Open(folderPath & wbFileName)
        With fromWorkbook.Worksheets(1)
            destSheet.Range("B8:G8").Offset(r).Value = .Range("Q10:V10").Value
            destSheet.Range("H8:M8").Offset(r).Value = .Range("Q13:V13").Value
            destSheet.Range("N8:S8").Offset(r).Value = .Range("Q16:V16").Value
            destSheet.Range("A8").Offset(r).Value = .Range("B14").Value
            r = r + 1
        End With
        fromWorkbook.Close savechanges:=False
        DoEvents
        wbFileName = Dir
    Wend
   
    Application.ScreenUpdating = True
   
    MsgBox "Finished"
   
End Sub
 
Upvote 0
Thank you so much John.

For the most part this worked well. Except now I realized that we have more than one format of the spreadsheet.

Instead of the B8:H8... is there a way to search for a particular heading and copy cells under it?

Thanks in advance.
 
Upvote 0
Try this

Change the names of header, path, destination sheet, source sheet.
Assume the headings are in row 7 and they all exist.

VBA Code:
Sub Retrieve_Data()
  Dim fName As String, fPath As String
  Dim wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet
  Dim heads As Variant, cols As Variant
  Dim f As Range, i As Long, lr As Long
  Application.ScreenUpdating = False
 
  fPath = "C:\Users\htn\"                      'your path
  heads = Array("Head1", "Head2", "Head3", "Head4") 'your headings
  Set sh1 = ThisWorkbook.Sheets("AVG")              'your destination sheet
 
  ReDim cols(0 To UBound(heads))
  For i = 0 To UBound(heads)
    cols(i) = sh1.Rows(7).Find(heads(i), , xlValues, xlWhole).Column
  Next
 
  lr = 8
  fName = Dir(fPath & "*.xls")
  Do While fName <> ""
    Set wb2 = Workbooks.Open(fPath & fName)
    Set sh2 = wb2.Sheets("Flow_Level")               'your source sheet
    sh1.Cells(lr, cols(0)).Resize(1, 6).Value = sh2.Range("Q10:V10").Value
    sh1.Cells(lr, cols(1)).Resize(1, 6).Value = sh2.Range("Q13:V13").Value
    sh1.Cells(lr, cols(2)).Resize(1, 6).Value = sh2.Range("Q16:V16").Value
    sh1.Cells(lr, cols(3)).Value = sh2.Range("B14").Value
    lr = lr + 1
    wb2.Close False
    fName = Dir()
  Loop
 
  MsgBox "End"
End Sub
 
Upvote 0
Thank you all. I found that there were multiple layouts of the spreadsheets and was able to modify the scripts to sort them out. I was able to use John's script for the remainder of the files and works well.

Thank again!
 
Upvote 0
Hi John_w
I was given your code below and solution from another Mr Excel expert - it works well and has solved my problem
Just one question though...the macro deletes the column headers from the destination sheet (destSheet) when the matching workbook data has been pasted in. Is there any amendment to the VBA that I can use to retain the column headers in the destSheet?
Thanks

Welcome to MrExcel forums.

Try this macro, changing the workbooks folder and filespec and destination sheet name where indicated.
VBA Code:
Public Sub Copy_Values_From_Workbooks()

    Dim matchWorkbooks As String
    Dim destSheet As Worksheet, r As Long
    Dim folderPath As String
    Dim wbFileName As String
    Dim fromWorkbook As Workbook
  
    'Folder path and wildcard workbook files to import cells from
  
    matchWorkbooks = "C:\folder\path\*.xls"                                             'CHANGE THIS
  
    'Define destination sheet
  
    Set destSheet = ActiveWorkbook.Worksheets("Summary")                                'CHANGE THIS
  
    destSheet.Cells.Clear
    r = 0
  
    Application.ScreenUpdating = False
          
    folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
    wbFileName = Dir(matchWorkbooks)
    While wbFileName <> vbNullString
        Set fromWorkbook = Workbooks.Open(folderPath & wbFileName)
        With fromWorkbook.Worksheets(1)
            destSheet.Range("B8:G8").Offset(r).Value = .Range("Q10:V10").Value
            destSheet.Range("H8:M8").Offset(r).Value = .Range("Q13:V13").Value
            destSheet.Range("N8:S8").Offset(r).Value = .Range("Q16:V16").Value
            destSheet.Range("A8").Offset(r).Value = .Range("B14").Value
            r = r + 1
        End With
        fromWorkbook.Close savechanges:=False
        DoEvents
        wbFileName = Dir
    Wend
  
    Application.ScreenUpdating = True
  
    MsgBox "Finished"
  
End Sub
 
Upvote 0
Welcome to MrExcel forums.

Try this macro, changing the workbooks folder and filespec and destination sheet name where indicated.
VBA Code:
Public Sub Copy_Values_From_Workbooks()

    Dim matchWorkbooks As String
    Dim destSheet As Worksheet, r As Long
    Dim folderPath As String
    Dim wbFileName As String
    Dim fromWorkbook As Workbook
  
    'Folder path and wildcard workbook files to import cells from
  
    matchWorkbooks = "C:\folder\path\*.xls"                                             'CHANGE THIS
  
    'Define destination sheet
  
    Set destSheet = ActiveWorkbook.Worksheets("Summary")                                'CHANGE THIS
  
    destSheet.Cells.Clear
    r = 0
  
    Application.ScreenUpdating = False
          
    folderPath = Left(matchWorkbooks, InStrRev(matchWorkbooks, "\"))
    wbFileName = Dir(matchWorkbooks)
    While wbFileName <> vbNullString
        Set fromWorkbook = Workbooks.Open(folderPath & wbFileName)
        With fromWorkbook.Worksheets(1)
            destSheet.Range("B8:G8").Offset(r).Value = .Range("Q10:V10").Value
            destSheet.Range("H8:M8").Offset(r).Value = .Range("Q13:V13").Value
            destSheet.Range("N8:S8").Offset(r).Value = .Range("Q16:V16").Value
            destSheet.Range("A8").Offset(r).Value = .Range("B14").Value
            r = r + 1
        End With
        fromWorkbook.Close savechanges:=False
        DoEvents
        wbFileName = Dir
    Wend
  
    Application.ScreenUpdating = True
  
    MsgBox "Finished"
  
End Sub



@John_w Hi, John, thanks for the code, its works fine for what Dreamwrx wanted to do. Could you help me do same same but for all the csv files in a folder. The destination and a bunch of origin files will be in the same folder and I want the data from specific cells to be copied from and pasted in the destination file in successsive rows. For example-

from origin_file_1.csv to the dest_file.xlsm
Q10:V10 -> B8:G8;
Q13:V13 -> H8:M8;
Q16:V16 -> N8-S8;
B14 -> A8

from origin_file_2.csv to the dest_file.xlsm
Q10:V10 -> B9:G9;
Q13:V13 -> H9:M9;
Q16:V16 -> N9:S9;
B14 -> A9

and so forth. Thanks in advance!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top