Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Copy a Range into a New Workbook

Tags:

excel

vba

I am a newbie to Excel VBA.

I am trying to copy a range of data from worksheet output into a new excel workbook and save the new workbook with the the value in E3.

As a bonus, I would love to be able to also copy the data into wordpad and save as E3.xml

Here is the VBA I have thus far:

Sub CopyOutput()
Dim myname As String
mystring = E3
Dim myselection As Range

myselection = Sheets("Output").Columns("F").Select
Set NewBook = Workbooks.Add
    With NewBook
        .SaveAs Filename:="C:\Program Files\White Plume\Scenarios\" & myname & ".xls", FileFormat:= _
             xlsx, CreateBackup:=False
    End With
myselection.Paste
End Sub
like image 480
user2872317 Avatar asked Oct 11 '13 19:10

user2872317


People also ask

How do I copy a range of cells from one workbook to another?

Copy a Cell or Range to Another Worksheet First, define the range or the cell that you want to copy. Next, type a dot (.) and select the copy method from the list of properties and methods. Here you'll get an intellisense to define the destination of the cell copied.

How do I copy a range of cells in Excel VBA?

Copy VBA method is to copy a particular range. When you copy a range of cells manually by, for example, using the “Ctrl + C” keyboard shortcut, the range of cells is copied to the Clipboard. You can use the Range. Copy method to achieve the same thing.

How do I copy VBA code from one workbook to another?

Open both the workbook that contains the macro you want to copy, and the workbook where you want to copy it. On the Developer tab, click Visual Basic to open the Visual Basic Editor. , or press CTRL+R . In the Project Explorer pane, drag the module containing the macro you want to copy to the destination workbook.

How do I automatically copy data from one workbook to another in Excel?

Copy all the data on the sheet by pressing CTRL+C. Open the workbook in which you want to paste the data, then click the + in the status bar to add a new blank worksheet. Click the first cell in the new worksheet, then press CTRL+V to paste the data into that worksheet.


1 Answers

Modify to suit your specifics, or make more generic as needed:

Private Sub CopyItOver()
  Set NewBook = Workbooks.Add
  Workbooks("Whatever.xlsx").Worksheets("output").Range("A1:K10").Copy
  NewBook.Worksheets("Sheet1").Range("A1").PasteSpecial (xlPasteValues)
  NewBook.SaveAs FileName:=NewBook.Worksheets("Sheet1").Range("E3").Value
End Sub
like image 54
Lance Roberts Avatar answered Oct 13 '22 03:10

Lance Roberts