VBA to copy data from files on a list and paste to specific sheet

aholmbach

New Member
Joined
Dec 30, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi and Happy New Year
I'm trying to create a macro with the purpose as follows:

Open a specific file based on a path in a list (Path, coloum B), copy data in the file from an area (A1:AO5000), paste it to a specific sheet in the Masterfile, sheetname comes from SheetName, coloum A, same row as the path, close file without saving

1640880121605.png



Continue to next row, untill the macro has looped through all rows on the list

The number of sheets/files varies from month to month.

A PowerQuery creates the list on which SheetName and Path are mentioned.
Based on the list, a macro creates the sheets in which data should be copied to.

I have managed to create a macro which opens all files on the list, but are missing the knowledge of the copy/paste part to the correct sheet, and closing the opened files without saving them

Hopefully someone knows a lot more about VBA than I done

Thanks a lot in advance

Happy New Year :-)
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I assumed that:

- This table is on sheet 1
- The sheetnames from which you have to copy are the same as where to paste

VBA Code:
Sub jec()
 Dim ar As Variant, i As Long
 ar = Sheets(1).Cells(1, 1).CurrentRegion
  
  For i = 2 To UBound(ar)
    With GetObject(ar(i, 2)).Sheets(ar(i, 1))
       ThisWorkbook.Sheets(ar(i, 1)).Range("A1:AO5000").Value = .Range("A1:AO5000").Value
      .Parent.Close 0
    End With
  Next
End Sub
 
Upvote 0
I assumed that:

- This table is on sheet 1
- The sheetnames from which you have to copy are the same as where to paste

VBA Code:
Sub jec()
 Dim ar As Variant, i As Long
 ar = Sheets(1).Cells(1, 1).CurrentRegion
 
  For i = 2 To UBound(ar)
    With GetObject(ar(i, 2)).Sheets(ar(i, 1))
       ThisWorkbook.Sheets(ar(i, 1)).Range("A1:AO5000").Value = .Range("A1:AO5000").Value
      .Parent.Close 0
    End With
  Next
End Sub
Hi JEC
Correct assumptions.
It works as it should, thank you very-very-very much for your help, its highly appreciated
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

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