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()
 
It occurred to me just now that since you are 'moving' the new sheets to another workbook the loop I provided wouldn't work as with each execution the number of sheets in the workbook would decrease. Thus you would need to adjust for that:

Code:
Dim iStart, iStop, iSheet as Integer

iSheet = ThisWorkBook.Sheets.Count + 1

'Execute Code to Create Your New Sheets

iStop = ThisWorkBook.Sheets.Count

For iStart = iSheet to iStop
     'Execute Code to move Sheets
     'Reference Sheets(iSheet) each time
next iStart


'The intent is to loop through as many times as necessary to get all the newly created sheets moved but you are always moving the sheet at the same position in the workbook.

-G
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,215,453
Messages
6,124,925
Members
449,195
Latest member
Stevenciu

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