Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 DOCUMENTSWORKBOOK(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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...