简体   繁体   中英

VBA - copy paste macro help needed

Need help with a macro that will open 5 different csv files and automatically auto copy paste 3 columns of data (starting from the 2nd row to about the 200th row). Then the data will be pasted into one worksheet that is open so each file is all on one row (side by side)...any help will be appreciated..

Sub Macro2() 
    'Assign variable name to Target workbook 
    Var1 = ActiveWorkbook.Name 
    'Assign variable name to Target range 
    Var1R = "H1" 
    'Open Source WorkBook 
    Application.Workbooks.Open ("C:\MY DOCUMENTS\WORKBOOK(B).xls") 
    'Assign variable name to Source workbook 
    Var2 = ActiveWorkbook.Name 
    Var2R = "WORKSHEET-1" 
    'Copy from Source to Target 
    Sheets(Var2R).Columns("F").EntireColumn.Copy _     
    Destination:=Workbooks(Var1).Sheets("Sheet1").Range(Var1R) 
    'Close Source WorkBook wo/Save 
    Workbooks(Var2).Close False 
End Sub

Here is a program that will do that. Obviously you'll have to modify the file-paths and ranges.

Sub copy_paste()
Dim filepaths
Dim twb As Workbook
Dim x As Long

Set twb = ThisWorkbook
filepaths = Array("C:\A.csv", "C:\B.csv", "C:\C.csv", "C:\D.csv", "C:\E.csv")

For x = 1 To UBound(filepaths)+1
    With Workbooks.Open(filepaths(x-1))
        .Sheets(1).Range("A2:C200").Copy twb.Sheets(1).Cells(2, 3 * x - 2)
        .Close False
    End With
Next x

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM