I want to be able to have a VBA command button that opens a csv file and then automatically copy various cells then paste them into a new workbook into different cell locations.
I want to copy the following cells from a source workbook and paste them into a new workbook in the following cells (see table below)
Source Workbook | New Workbook |
---|---|
A2 | C8 |
B2 | F7 |
C2 | F6 |
E2 | F8 |
D2 | G8 |
I have this working code but it takes too long to execute, what can i do to stream line it?
Private Sub CommandButton3_Click()
FileToOpen = Application.GetOpenFilename(FileFilter:="Excel workbook(*.csv),*.csv", Title:="Open Report File")
Dim sourceworkbook As Workbook
Dim currentworkbook As Workbook
Set currentworkbook = ThisWorkbook
Set sourceworkbook = Application.Workbooks.Open(FileToOpen)
With sourceworkbook
sourceworkbook.Worksheets(1).Range("C2"").Copy
currentworkbook.Activate
currentworkbook.Worksheets("Sheet1").Activate
currentworkbook.Worksheets("Sheet1").Range("F6").Select
sourceworkbook.Worksheets(1).Range("C2").Copy
ActiveSheet.Paste
End With
With sourceworkbook
sourceworkbook.Worksheets(1).Range("B2").Copy
currentworkbook.Activate
currentworkbook.Worksheets("Sheet1").Activate
currentworkbook.Worksheets("Sheet1").Range("F7").Select
sourceworkbook.Worksheets(1).Range("B2").Copy
ActiveSheet.Paste
End With
With sourceworkbook
sourceworkbook.Worksheets(1).Range("E2").Copy
currentworkbook.Activate
currentworkbook.Worksheets("Sheet1").Activate
currentworkbook.Worksheets("Sheet1").Range("F8").Select
sourceworkbook.Worksheets(1).Range("E2").Copy
ActiveSheet.Paste
End With
With sourceworkbook
sourceworkbook.Worksheets(1).Range("D2").Copy
currentworkbook.Activate
currentworkbook.Worksheets("Sheet1").Activate
currentworkbook.Worksheets("Sheet1").Range("G8").Select
sourceworkbook.Worksheets(1).Range("D2").Copy
ActiveSheet.Paste
End With
With sourceworkbook
sourceworkbook.Worksheets(1).Range("A2").Copy
currentworkbook.Activate
currentworkbook.Worksheets("Sheet1").Activate
currentworkbook.Worksheets("Sheet1").Range("C8").Select
sourceworkbook.Worksheets(1).Range("A2").Copy
ActiveSheet.Paste
End With
sourceworkbook.Close
End Sub