VBA to Copy last 3 Spreadsheets to a new Workbook

avd88

Board Regular
Joined
Jan 18, 2016
Messages
112
Hello, I am trying to create a macro which copies and pastes the last 3 Spreadsheets of a Workbook into a new one. Anyone has any ideas of how this can be accomplished? What I've tried so far does not work.

Thank you in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
This is the general idea

Code:
Sub t()
Dim Oldwb As Workbook, Newwb As Workbook
Set Oldwb = Workbooks(1)
Set Newwb = Workbooks(2)
    For i = 1 To 3
        Oldwb.Sheets(Oldwb.Sheets.Count - i).Copy After:=Newwb.Sheets(Sheets.Count)
    Next
End Sub
 
Last edited:
Upvote 0
hi,
another way maybe:

Code:
Sub CopySheets()


    Dim wbNew As Workbook


    Set wbNew = Workbooks.Add(1)
    With ThisWorkbook
        .Worksheets(Array(.Sheets.Count, .Sheets.Count - 1, .Sheets.Count - 2)).Copy _
                                            After:=wbNew.Sheets(wbNew.Sheets.Count)
    End With


End Sub

Dave
 
Upvote 0
Thank you very much!

I ended up using ideas from both responses this is my code and it works:

Code:
'COPY AND SAVE'

Dim Oldwb As Workbook, Newwb As Workbook
Set Oldwb = Workbooks("Saxco COSR Test 25.xlsm")
Set Newwb = Workbooks.Add(1)




    For i = 1 To 3
        Oldwb.Sheets(Oldwb.Sheets.Count + 1 - i).Copy Before:=Newwb.Sheets(Sheets.Count)
    Next


Oldwb.Close SaveChanges:=True
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top