I have a workbook of 50+ worksheets. The worksheet names are 100, 101, 102 ... 199, 200, 201... 299 etc...and "Data". I need to group and SaveAs all "100", "200", etc worksheets along with the Data tab (to calculate all the formulas) as individual workbooks.
I have been searching and tried many methods, but nothing has work. Could someone please advise this is possible?
SaveAs Multiple Worksheets into Multiple Workbooks
-
-
-
Re: SaveAs Multiple Worksheets into Multiple Workbooks
I am not sure where (or when) exactly you want to invoke the code to copy these "groups" of worksheets into new workbooks but the following code should at least point you in the right direction of how to achieve this:
Code
Display MoreSub CopySheetsToNewWorkbooks() Dim wSheet As Worksheet Dim sBook1 As String, sBook2 As String sBook1 = vbNullString sBook2 = vbNullString On Error Resume Next For Each wSheet In ThisWorkbook.Sheets Select Case wSheet.Name Case "100" To "199" If sBook1 = vbNullString Then 'copy the "data" sheet to the new workbook ThisWorkbook.Sheets("Data").Copy 'and grab the name of the new workbook sBook1 = ActiveWorkbook.Name End If With Workbooks(sBook1) wSheet.Copy After:=.Sheets(.Worksheets.Count) End With Case "200" To "299" If sBook2 = vbNullString Then 'copy the "data" sheet to the new workbook ThisWorkbook.Sheets("Data").Copy 'and grab the name of the new workbook sBook2 = ActiveWorkbook.Name End If With Workbooks(sBook2) wSheet.Copy After:=.Sheets(.Worksheets.Count) End With End Select Err.Clear Next On Error GoTo 0 End Sub
One option would be to place this code in the Workbook_BeforeClose or Workbook_BeforeSave routines.
Also, if you wanted to move rather than copy the sheets, you would need to change the wSheet.Copy to a wSheet.Move (I am assuming you don't want to move the "Data" sheet, just copy it).
-
Re: SaveAs Multiple Worksheets into Multiple Workbooks
Hi gijsmo, thank you for writing this code. If possible, I want to save these new workbooks with the name "100", "200" etc..."commission month"
Also, as I am testing the marco, I notice there are a few more sheets I need to copy over as well in order for the calculation to work. Here is a list of tabs I need to bring over..
"Data"
"Input"
"Assumptions"
"AE Flu Tracings"
These tabs need to be hidden too.
Thanks! -
Re: SaveAs Multiple Worksheets into Multiple Workbooks
KAT11220
I have updated the code to accommodate what you asked, the only thing I didn't understand was the "commission month". However, if it relates to changing the final workbook name from "100", "200", etc to "100-Jun-11", "200-Jun-11", then it won't be hard to see where this needs to be done:
Code
Display MoreSub CopySheetsToNewWorkbooks() Dim wSheet As Worksheet Dim sBook(1 To 2) As String Dim i As Integer For i = 1 To 2 sBook(i) = vbNullString Next On Error Resume Next With Application .ScreenUpdating = False .EnableEvents = False .DisplayAlerts = False End With If ActiveWorkbook.Name <> ThisWorkbook.Name Then ThisWorkbook.Activate For Each wSheet In ThisWorkbook.Sheets Select Case wSheet.Name Case "100" To "199" If sBook(1) = vbNullString Then 'copy the "necessary" sheets to the new workbook CopyBaseSheets sBook(1) End If With Workbooks(sBook(1)) wSheet.Copy After:=.Sheets(.Worksheets.Count) End With Case "200" To "299" If sBook(2) = vbNullString Then 'copy the "necessary" sheets to the new workbook CopyBaseSheets sBook(2) End If With Workbooks(sBook(2)) wSheet.Copy After:=.Sheets(.Worksheets.Count) End With End Select Err.Clear Next 'now we can try and hide the "Data" sheet in the new workbooks 'this will work as long as there are other visible sheets in the workbook 'we will also try and save the new workbook For i = 1 To 2 If sBook(i) <> vbNullString Then With Workbooks(sBook(i)) .Sheets("Data").Visible = xlSheetHidden 'use the counter value (i) to set the beginning of the name 'if the file already exists, it will be overwritten as Application.DisplayAlerts is False 'the file will be saved to the current default folder 'if the file needs to be saved in another folder, then the filename should be fully qualified 'eg, "C:\MyDocs\Excel\" & i & "00.xls" .SaveAs Filename:=i & "00.xls", _ FileFormat:=xlNormal, Password:=vbNullString, WriteResPassword:=vbNullString, _ ReadOnlyRecommended:=False, CreateBackup:=False End With End If Next On Error GoTo 0 With Application .ScreenUpdating = True .EnableEvents = True .DisplayAlerts = True End With End Sub Private Sub CopyBaseSheets(wBookName As String) 'this will copy all the 'base' sheets to a new workbook and set the name of the new workbook wBookName = vbNullString With ThisWorkbook CopyHiddenSheet "Data", wBookName CopyHiddenSheet "Input", wBookName CopyHiddenSheet "Assumptions", wBookName CopyHiddenSheet "AE Flu Tracings", wBookName End With End Sub Private Sub CopyHiddenSheet(ByVal sShtName As String, Optional wBookName As String = vbNullString) Dim fHidden As Boolean On Error GoTo QuickExit With ThisWorkbook 'unhide the source sheet if necessary and copy it fHidden = .Sheets(sShtName).Visible .Sheets(sShtName).Visible = xlSheetVisible 'if we have no workbook name yet, we are starting a new workbook If wBookName = vbNullString Then .Sheets(sShtName).Copy wBookName = ActiveWorkbook.Name Else 'otherwise we are adding sheets to the newly created workbook .Sheets(sShtName).Copy After:=Workbooks(wBookName).Sheets(Workbooks(wBookName).Worksheets.Count) End If 'now hide the sheet again if necessary .Sheets(sShtName).Visible = fHidden End With 'we can hide everything except the first sheet ("Data") in the new workbook With Workbooks(wBookName) If .Worksheets.Count > 1 Then .Sheets(.Worksheets.Count).Visible = xlSheetHidden End If End With QuickExit: End Sub
The code is a bit more complex now as there are additional subs required to perform the task. The main sub CopySheetsToNewWorkbooks is still the one you need to call to perform the work.
Hopefully, the comments will make the rest self-explanatory! And hopefully, you can see how this can be relatively easily expanded to include "300", "400", etc.
-
Re: SaveAs Multiple Worksheets into Multiple Workbooks
gijsmo,
I think we are almost there!!! My only issue is that because every sheet is copy individually, the formula is referencing the master workbook and not the new "data" tabs copied. Is there a way to copy the group of worksheets together? Or a code to reference to the new workbooks?
Thanks! -
Re: SaveAs Multiple Worksheets into Multiple Workbooks
I don't have a quick VBA solution right now but one way to fix this I think is to edit the links. In Excel 2003, select Edit --> Links on the newly created spreadsheets. Then select the Change Source option and change the source of the links from the Master spreadsheet to the newly created spreadsheet (essentially point the links back to itself). If this is successful, the Edit --> Links option will no longer be available in the new spreadsheet as there are no longer any external links.
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!