Save and delete extra worksheets

NMGMX

New Member
Joined
Jul 18, 2014
Messages
10
I have a workbook and want to delete all sheets after Worksheet X (orderwise not namewise). But first I want to save them to a separate workbook. What is the best way to do this?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Rather than save and delete, move them to a new workbook and then save the new workbook

Code:
sname = ActiveWorkbook.Name 'Source workbook

Workbooks.Add 

tName = ActiveWorkbook.Name  'Target workbook

For n = 4 To 6 ' whatever your criteria

    Workbooks(sname).Sheets(n).Move After:=Workbooks(tName).Sheets(Sheets.Count - 1)

Next n
 
Upvote 0
For the life of me I cannot figure out why the code won't work!
Code:
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=WorkbookNm & " (Misfit Categories)"
    mname = ActiveWorkbook.Name
For n = 33 To Workbooks(WorkbookNm & ".xlsm").Sheets.Count
    MsgBox Workbooks(WorkbookNm & ".xlsm").Sheets(n).Name
    Workbooks(WorkbookNm & ".xlsm").Sheets(n).Move After:=Workbooks(mname).Sheets(Sheets.Count - 1)
Next n
I get no error, and the new workbook appears, but it doesn't have my sheets. The MsgBox returns the right column(s), but for some reason it looks like they don't move over, why?
 
Upvote 0
That's because it is always referencing the new workbook. Define the source name before you add the workbook


Code:
SourceName = ActiveWorkbook.Name 'Name of the workbook you are moving the sheets from

Workbooks.Add

ActiveWorkbook.SaveAs Filename:=WorkbookNm & " (Misfit Categories)"
    mname = ActiveWorkbook.Name

For n = 33 To Workbooks(SourceName).Sheets.Count
    MsgBox Workbooks(SourceName).Sheets(n).Name
    Workbooks(SourceName).Sheets(n).Move After:=Workbooks(mname).Sheets(Sheets.Count - 1)
Next n
 
Upvote 0
That's because it is always referencing the new workbook. Define the source name before you add the workbook


Code:
SourceName = ActiveWorkbook.Name 'Name of the workbook you are moving the sheets from

Workbooks.Add
ActiveWorkbook.SaveAs Filename:=WorkbookNm & " (Misfit Categories)"
    mname = ActiveWorkbook.Name

For n = 33 To Workbooks(SourceName).Sheets.Count
    MsgBox Workbooks(SourceName).Sheets(n).Name
    Workbooks(SourceName).Sheets(n).Move After:=Workbooks(mname).Sheets(Sheets.Count - 1)
Next n
It actually wasn't. It used WorkbookNm to refer to the original worksheet. I actually ended up using Sheets(Sheets.Count) instead of Sheets(Sheets.Count - 1), and it worked. Thanks anyways.
Final Code:
Code:
    Workbooks.Add    ActiveWorkbook.SaveAs Filename:=WorkbookNm & " (Misfit Categories)"
    Dim mname As String
    mname = ActiveWorkbook.Name
For n = 33 To Workbooks(WorkbookNm & ".xlsm").Sheets.Count
    Workbooks(WorkbookNm & ".xlsm").Sheets(n).Move After:=Workbooks(mname).Sheets(Sheets.Count)
    Workbooks(WorkbookNm & ".xlsm").Sheets(n).Delete
Next n
    Workbooks(mname).Sheets(1).Delete
    Workbooks(mname).Save
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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