copy data from multiple workbooks

wells

New Member
Joined
Jan 9, 2020
Messages
24
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
hi ,

Im new to VBA and I have been trying to create a program to copy specific range from multiple workbooks having data in sheet 2 to a master workbook sheet 2 .

COPY Condition: the column range will be A20 to AS20 while the row range will depend upon the last cell having data in column R.

PASTE Condition: consecutively all copied cells should be pasted with one blank row in between.


I came till the below stage, but no idea to proceed further. made some corrections but didnt work well.

Prog:

VBA Code:
Sub copyDataFromMultipleWorkbooksIntoMaster()

Dim FolderPath As String, Filepath As String, Filename As String

FolderPath = "C:\Users\Desktop\combine\"

Filepath = FolderPath & "*.xlsx*"

Filename = Dir(Filepath)

Dim lastrow As Long, lastcolumn As Long

Do While Filename <> ""
Workbooks.Open (FolderPath & Filename)

lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, lastcolumn))

Filename = Dir

Loop
Application.DisplayAlerts = True
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Assuming you are running the code from the master workbook.

Code:
Sub copyDataFromMultipleWorkbooksIntoMaster()
Dim FolderPath As String, Filepath As String, Filename As String, wb As Workbook
FolderPath = "C:\Users\Desktop\combine\"
Filepath = FolderPath & "*.xlsx*"
Filename = Dir(Filepath)
Dim lastrow As Long, lastcolumn As Long
    Do While Filename <> ""
        Set wb = Workbooks.Open(FolderPath & Filename)
        lastrow = ActiveSheet.Cells(Rows.Count, "R").End(xlUp).Row
        lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
        Range(Cells(20, 1), Cells(lastrow, lastcolumn)).Copy ThisWorkbook.Sheets(2).Cells(Rows.Count, 1)(3)
        wb.Close False
        Filename = Dir
    Loop
End Sub
 
Upvote 0
hi,

Im getting the below error.

1578639065352.png
 
Upvote 0
Remove the asterisk after the file extension .xlsx as shown below.
Code:
Filepath = FolderPath & "*.xlsx"
The wild card symbol after the file extension world make VBA expect to find something and Excel does not recognize a file extension greter than four characters. I missed that on the first go round.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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