Macro to copy data from closed excel files in different folders using folder and file name as criteria in destination table

engrph

New Member
Joined
Oct 23, 2017
Messages
4
I have a table with column B (folder name) and column C (file name) as the criteria.
Tables D to G are imported data say,"H2:K5", from other closed excel files saved in different folders.

I have a hundred of these files that are updated from time to time and I need to gather it all at once. Any macro ideas for these? Thank you.

ETEFNcE.png
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I'm starting with this macro but I want it to loop through all the rows and stop which column B & C are blank. I'm still new with VBA. Your help is much appreciated!

Code:
Sub ImportData()
'Defines variables
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim sourceworkbook As String
' Define which workbook is which
Set wb1 = ThisWorkbook
' Note: File directory(C:\Users\engrph\Folder1\b100.xlsm) is inputted in cell "A2" in wb1 "Data1" sheet
sourceworkbook = ThisWorkbook.Worksheets("Data1").Range("A2")
Set wb2 = Workbooks.Open(sourceworkbook)
' Copy range "H2:K5" from the "Data2" sheet of wb2
wb2.Sheets("Data2").Range("H2:K5").Copy
' Paste the copied data to range "D2" of the "Data1" sheet in wb1
wb1.Sheets("Data1").Activate
    Range("D2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
' Save and Close wb2
wb2.Save
wb2.Close      
End Sub
 
Upvote 0
Welcome to the forum!

(1) Yes, one can set the values on a run to open each workbook and fill in the values. This gives you flexibility to not insert 0's or such at that time. It can also add formats.

(2) Or, it could probably add the reference as an array formula in the macro run so that it stays current without action. Of course this method has some overhead as links would update on workbook open but than might be a good thing.

(2a) One might want to also use a sheet change event to update the array formula when B or C column values change.

Which would you like?
 
Last edited:
Upvote 0
Welcome to the forum!

(1) Yes, one can set the values on a run to open each workbook and fill in the values. This gives you flexibility to not insert 0's or such at that time. It can also add formats.

(2) Or, it could probably add the reference as an array formula in the macro run so that it stays current without action. Of course this method has some overhead as links would update on workbook open but than might be a good thing.

(2a) One might want to also use a sheet change event to update the array formula when B or C column values change.

Which would you like?

Thanks Kenneth!

I think option (1) is what I'm looking for. Kindly direct me with the proper codes.
 
Upvote 0
With threaded posts, there is seldom a need to fully quote a post. Just say, regarding post #2 if not responding directly after it.

Change the inputs and the sheet and ranges in GetObject() to copy to suit.

Always test on backup copy. Add to a Module:
Code:
Sub CopyFromClosedWorkbooks()
  Dim ws As Worksheet, b As Range, c As Range, p$, fn$
  Dim fso As Object, calc As Integer
  
  'Change INPUTS
  p = "C:\Users\lenovo1\Dropbox\Excel\Excel4Macro\"
  Set ws = Sheet2
  
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  calc = Application.Calculation
  Application.Calculation = xlCalculationManual
  
  With ws
    Set b = .Range("B2", .Cells(.Rows.Count, "B").End(xlUp))
  End With
  Set b = b.SpecialCells(xlCellTypeConstants)
  If b Is Nothing Then Exit Sub
  
  Set fso = CreateObject("Scripting.FileSystemObject")
   
  For Each c In b
    fn = p & c & "\" & c.Offset(, 1).Value
    If Not fso.FileExists(fn) Then GoTo NextC
    GetObject(fn).Worksheets("Sheet1").Range("A1:C3").Copy _
      c.Offset(, 2).Resize(3, 3)
NextC:
  Next c
  
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Application.Calculation = calc
  Application.CutCopyMode = False
  Set fso = Nothing
End Sub
 
Last edited:
Upvote 0
Add this after the GetObject().
Code:
Workbooks(c.Offset(, 1).Value).Close False
 
Upvote 0
^
I have tried to modify the code but I get runtime error 1004: No cells were found on this particular line:
Code:
  Set aj = aj.SpecialCells(xlCellTypeConstants)

Here's the full code:
Code:
Sub Test4()
    Dim ws As Worksheet, aj As Range, ak As Range, p$, fn$
    Dim fso As Object, calc As Integer
    
    'Common file path inputted in cell AJ17
    p = ThisWorkbook.Worksheets("Flexure").Range("AJ17")
    Set ws = Sheet2
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    calc = Application.Calculation
    Application.Calculation = xlCalculationManual
With ws
    Set aj = .Range("AJ18", .Cells(.Rows.Count, "AJ").End(xlUp))
  End With
  Set aj = aj.SpecialCells(xlCellTypeConstants)
  If aj Is Nothing Then Exit Sub
  
  Set fso = CreateObject("Scripting.FileSystemObject")
   
  For Each ak In aj
    fn = p & ak & "" & ak.Offset(, 1).Value
    If Not fso.FileExists(fn) Then GoTo NextC
    GetObject(fn).Worksheets("Beam Design Forces").Range("AJ7:AL12").Copy _
      ak.Offset(, -34).Resize(6, 3)
NextC:
  Next ak
  
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = calc
    Application.CutCopyMode = False
    Set fso = Nothing
  
    ' Message if finished
    MsgBox "Completed!"
End Sub

Below figure shows what I'm trying to achieve.

From: B1_RB-1.xlsm, AJ7:AL12
To: Summary.xlsm, C18:E23

From B17_RB-2.xlsm, AJ7:AL12
To: Summary.xlsm, C24:E29

..and so on and so forth..

FyVkJpI.png
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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