James Page
James Page

Reputation: 27

Copy specific columns into new workbook

I have a workbook with 2 sheets, I am only concerned with Sheet1 for this function.

I copy the data from columns A and G into a new workbook using the following code I found on this site.

Sub dural()
Dim r1 As Range, r2 As Range
Sheets(1).Select
Set r1 = Range("A:A")
Set r2 = Range("G:G")
Set wbNew = Workbooks.Add
r2.Copy Range("A1")
r1.Copy Range("B1")
End Sub

I don't need the new workbook to open and be displayed like the above code does.

I would like to automatically create and save the output as a new file, preferably in a specific directory. Lets call it output.xlsx. The original file is SOURCE.xlsm

Upvotes: 0

Views: 9991

Answers (1)

Dirk Reichel
Dirk Reichel

Reputation: 7979

Something simple like this:

Sub dural()
  With Workbooks.Add
    ThisWorkbook.Sheets(1).Range("G:G").Copy .Sheets(1).Range("A1")
    ThisWorkbook.Sheets(1).Range("A:A").Copy .Sheets(1).Range("B1")
    .SaveAs "your path + filename here"
    .Close
  End With
End Sub

should do all you want.

If you still have any questions, just ask :)

Upvotes: 1

Related Questions