Moving unknown sheets to new workbook

preetx2

New Member
Joined
Jun 21, 2016
Messages
6
I want to move multiple sheets to new workbook. Currently, I am using the code posted below and it works just fine. However, its limited. Is there a way, Where I can count the sheets in the workbook first, then run the code to generate new worksheets and move only newly generated sheets to new workbook? Basically, I am trying to avoid listing the name of all sheets.

sub move()
Dim w As Workbook, ws As Worksheet, ss As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.CodeName <> "Sheet1" And ws.CodeName <> "Sheet2" And ws.CodeName <> "Sheet3" And ws.CodeName <> "Sheet4" And ws.CodeName <> _
"Sheet5" And ws.CodeName <> "Sheet6" And ws.CodeName <> "Sheet7" And ws.CodeName <> "Sheet8" And ws.CodeName <> "Sheet9" And ws.CodeName <> _
"Sheet10" And ws.CodeName <> "Sheet11" And ws.CodeName <> "Sheet12" And ws.CodeName <> "Sheet13" And ws.CodeName <> "Sheet14" And ws.CodeName <> "Sheet15" And ws.CodeName <> _
"Sheet16" And ws.CodeName <> "Sheet17" And ws.CodeName <> "Sheet18" And ws.CodeName <> "Sheet19" And ws.CodeName <> "Sheet20" And ws.CodeName <> "Sheet21" And ws.CodeName <> "Sheet22" Then


If w Is Nothing Then
ws.move


Set w = ActiveWorkbook
Else
ws.move After:=ss
End If
Set ss = ActiveSheet
End If
Next ws
Sheets(1).Select
ThisWorkbook.Activate
end sub()
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Code:
Sheets.Count
wil give you total number of sheets in your active workbook. if it is what you looking for

If you wanna adda sheet at the end of last sheet :

Code:
Dim ws As Worksheet

 With ActiveWorkbook
        Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        ws.Name = "YourNewSheetName"
    End With
 
Last edited:
Upvote 0
Code:
Sheets.Count
wil give you total number of sheets in your active workbook. if it is what you looking for

If you wanna adda sheet at the end of last sheet :

Code:
Dim ws As Worksheet

 With ActiveWorkbook
        Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
        ws.Name = "YourNewSheetName"
    End With



Thanks, but I am looking to move sheets to new workbook. I am generating about 50 new sheets from template that needs to be moved out of current workbook. Current workbook has 22 worksheet that needs to stay in the current workbook, only needs to move the new sheets generated from template.
 
Upvote 0
How/when are you generating the new worksheets?
 
Upvote 0
You can reference sheets in your workbook numerically from left to right with 1 being the leftmost sheet incrementing by 1 each sheet to the right. New sheets created programmatically should appear to the right so since you are keeping the first 22 sheets you would just run a quick loop to move all sheets numbered 23 and higher to the new workbook.

Have you tried doing that?
 
Upvote 0
I am selected partner's initials and generating new sheets based on the template. The template is linked to other 20 tabs in the file and generating a formula. So i need those tabs in there, but anything that is generated after those tabs needs to be moved into new workbook.


Allocations.JPG
 
Upvote 0
I did. I did an array with the sheets, but again its limited. Its a quarter based file, that 22 number could change over quarter. Hence I was looking for where I can count the sheets first in the workbook, and then anything number higher than that will be moved to new workbook.
 
Upvote 0
You can reference sheets in your workbook numerically from left to right with 1 being the leftmost sheet incrementing by 1 each sheet to the right. New sheets created programmatically should appear to the right so since you are keeping the first 22 sheets you would just run a quick loop to move all sheets numbered 23 and higher to the new workbook.

Have you tried doing that?

I did. I did an array with the sheets, but again its limited. Its a quarter based file, that 22 number could change over quarter. Hence I was looking for where I can count the sheets first in the workbook, and then anything number higher than that will be moved to new workbook.​




 
Upvote 0
How is the array limited?

How did you actually use one?
 
Upvote 0
I did. I did an array with the sheets, but again its limited. Its a quarter based file, that 22 number could change over quarter. Hence I was looking for where I can count the sheets first in the workbook, and then anything number higher than that will be moved to new workbook.​




You can use the Sheets.Count method first to get your sheet count then in your loop set your control variable (i.e. i) to Sheets.Count + 1. After the new sheets are created execute Sheets.Count a second time and either set it to a variable to represent the stop point of the loop or juts put that in the call for the loop. i.e.

Code:
dim iStart, iStop as integer

iStart = ThisWorkbook.Sheets.Count + 1

'Execute Code to Create your new sheets

iStop = ThisWorkbook.Sheets.Count

for iStart to iStop 'Or "For iStart to ThisWorkbook.Sheets.Count
     'Execute code to move sheet(iStart) to new workbook
next iStart

Make sense?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,713
Members
449,464
Latest member
againofsoul

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