VBA code to copy and paste into workbook?

tad_excel_74

New Member
Joined
Jul 17, 2019
Messages
3
What VBA code can copy and paste data in separate workbooks all in the same file folder with the same column headings and all on Sheet 1 into a separate workbook?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Save the file you want the data copied to as a macro enabled workbook (.xlsm) in the same directory as the files to be copied and copy the code below into the standard code module1 of that workbook. The code assumes that each workbook uses Sheet 1 for data content.
Code:
Sub t()
Dim wb As Workbook, sh As Worksheet, fPath As String, fName As String
Set sh = ThisWorkbook.Sheets("Sheet1") 'Edit sheet name
fPath = ThisWorkbook.Path & "\"
fName = Dir(fPath & "*.xl*")
    Do While fName <> ""
        If fName <> ThisWorkbook.Name Then
            Application.DisplayAlerts = False
            Set wb = Workbooks.Open(fPath & fName)
            wb.Sheets(1).UsedRange.Offset(1).Copy 'Don't copy headers
            sh.Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial xlPasteValues 'Don't copy formulas
            wb.Close False
            Application.DisplayAlerts = True
        End If
        fName = Dir
    Loop
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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