Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy only a single worksheet to another workbook using vba

Tags:

excel

vba

I have 1 WorkBook("SOURCE") that contains around 20 Sheets.
I want to copy only 1 particular sheet to another Workbook("TARGET") using Excel VBA.

Please note that the "TARGET" Workbook doen't exist yet. It should be created at runtime.

Methods Used -

1) Activeworkbook.SaveAs <--- Doesn't work. This will copy all the sheets. I want only specific sheet.

like image 281
Solution Seeker Avatar asked Nov 27 '13 15:11

Solution Seeker


People also ask

How do you copy a range of cells from one sheet to another in Excel VBA?

To copy a cell or a range of cells to another worksheet you need to use the VBA's “Copy” method. In this method, you need to define the range or the cell using the range object that you wish to copy and then define another worksheet along with the range where you want to paste it.

How do I copy a worksheet to another workbook?

Copy a sheet to another workbook Click the sheet that you want to copy. On the Edit menu, click Sheet > Move or Copy Sheet. On the To book menu, click the workbook that you want to copy the sheet to. Tip: To create a new workbook that contains the moved sheet, click new book.


1 Answers

I have 1 WorkBook("SOURCE") that contains around 20 Sheets. I want to copy only 1 particular sheet to another Workbook("TARGET") using Excel VBA. Please note that the "TARGET" Workbook doen't exist yet. It should be created at runtime.

Another Way

Sub Sample()
    '~~> Change Sheet1 to the relevant sheet
    '~~> This will create a new workbook with the relevant sheet
    ThisWorkbook.Sheets("Sheet1").Copy

    '~~> Save the new workbook
    ActiveWorkbook.SaveAs "C:\Target.xlsx", FileFormat:=51
End Sub

This will automatically create a new workbook called Target.xlsx with the relevant sheet

like image 138
Siddharth Rout Avatar answered Oct 29 '22 11:10

Siddharth Rout