Splitting specific worksheets and saving them into new workbooks!

cutemeatball

New Member
Joined
Jun 13, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello,

So I tried to make my own macro below and it doesn't seem to work. Gives me the error 9 message. Can anyone assist? Basically I am trying to split specific worksheets from a workbook and save them as a new workbooks (multiple workbooks).

VBA Code:
Sub SplitTechnologies()

Call SheetstoBooksButch
Call SheetstoBooksSecurity
Call SheetstoBooksAV
Call SheetstoBooksDAS

End Sub



Sub SheetstoBooksButch()



ThisWorkbook.Sheets(Array("Security", "AV", "Wireless", "SEC & AV", "Security RSS", "AV RSS", "SEC & AV RSS", "AV Division", "Wireless Division")).Copy
ActiveWorkbook.SaveAs "SEC AV DAS"




End Sub

Sub SheetstoBooksSecurity()


ThisWorkbook.Sheets(Array("Security", "Security RSS")).Copy
ActiveWorkbook.SaveAs "SEC"




End Sub

Sub SheetstoBooksAV()


ThisWorkbook.Sheets(Array("AV", "AV RSS", "AV Division")).Copy
ActiveWorkbook.SaveAs "AV"




End Sub

Sub SheetstoBooksDAS()


ThisWorkbook.Sheets(Array("Wireless", "Wireless Division", "Burkhart", "Barnhill", "Allen", "Finger", "McCallum")).Copy
ActiveWorkbook.SaveAs "AV"




End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
The macro recorder is useful for this. It generated:
VBA Code:
Sub Macro1()
    Sheets(Array("AV", "AV RSS", "AV Division")).Select
    Sheets("AV RSS").Activate
    Sheets(Array("AV", "AV RSS", "AV Division")).Copy
    ChDir "C:\Temp"
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
which can be refined by removing unnecessary lines, closing the new workbook and unselecting the multiple sheets:
VBA Code:
Sub Macro1()
    Sheets(Array("AV", "AV RSS", "AV Division")).Copy
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close False
    Sheets("AV").Select
End Sub
 
Upvote 0
Hi,

So I tried your refined macro and it gave me a error 9 and it took me to the first line Sheets(Array*****

Can you advise? Thanks!

VBA Code:
Sub ALLsplits()

Call Butchworkbook
Call SECworkbook
Call AVworkbook
Call DASworkbook

End Sub



Sub Butchworkbook()


    Sheets(Array("Security", "AV", "Wireless", "SEC & AV", "Security RSS", "AV RSS", "SEC & AV RSS", "AV Division", "Wireless Division")).Copy
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close False
    Sheets("SEC AV DAS").Select


End Sub

Sub SECworkbook()


    Sheets(Array("Security", "Security RSS")).Copy
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close False
    Sheets("SEC").Select


End Sub

Sub AVworkbook()

    Sheets(Array("AV", "AV RSS", "AV Division")).Copy
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close False
    Sheets("AV").Select
    
End Sub

Sub DASworkbook()



    Sheets(Array("Wireless", "Wireless Division", "Burkhart", "Barnhill", "Allen", "Finger", "McCallum")).Copy
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close False
    Sheets("DAS").Select





End Sub









The macro recorder is useful for this. It generated:
VBA Code:
Sub Macro1()
    Sheets(Array("AV", "AV RSS", "AV Division")).Select
    Sheets("AV RSS").Activate
    Sheets(Array("AV", "AV RSS", "AV Division")).Copy
    ChDir "C:\Temp"
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
which can be refined by removing unnecessary lines, closing the new workbook and unselecting the multiple sheets:
VBA Code:
Sub Macro1()
    Sheets(Array("AV", "AV RSS", "AV Division")).Copy
    ActiveWorkbook.SaveAs Filename:="C:\Temp\AV.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close False
    Sheets("AV").Select
End Sub
[/CODE
[/QUOTE]
 
Upvote 0
In which macro? Are the sheet names correct?

All the macros are saving as "C:\Temp\AV.xlsx"
Wait I'm so dumb I think I got the first part down now. Now the error shows here:

1656011107403.png


1656011362483.png
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,888
Members
449,097
Latest member
dbomb1414

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