Repeat this macro for all sheets in a workbook

rjdrury

New Member
Joined
Aug 3, 2018
Messages
3
How to I change the code below so it will run for all sheets in the workbook?

Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.Cells.Copy
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteValues
ActiveSheet.Cells.PasteSpecial Paste:=xlPasteFormats
ActiveWorkbook.Saveas _
Filename:=MyPath & "" & sht.Name & ".xls"
With Application
.DisplayAlerts = False
.ScreenUpdating = False
With ActiveWorkbook
.Saveas FileFormat:=xlNormal, Password:="2012270400", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
.Close
End With
.ScreenUpdating = True
.DisplayAlerts = True
End With
ActiveWorkbook.Close savechanges:=False
Next sht
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Welcome to the Board!

Try changing all your references of "ActiveSheet." to "sht."
Since you are not selecting any sheets in your code, ActiveSheet will always refer to the sheet that was active when the code started.
 
Upvote 0
Welcome to the Board!

Try changing all your references of "ActiveSheet." to "sht."
Since you are not selecting any sheets in your code, ActiveSheet will always refer to the sheet that was active when the code started.

Still stops running after the first sheet is processed
 
Upvote 0
That is because you are closing the activeworkbook.

What exactly are trying to do? Are you trying to copy each sheet to a separate workbook?
If so, you need to create a new workbook to copy into. It looks like you are just copying the file onto itself and closing it.
 
Upvote 0
Thanks for the reply.

I am trying to split the sheets in one workbook into separate workbooks and save those workbooks with a password.

I found some new code on the web for splitting the worksheets into separate workbooks and inserted the line ActiveWorkbook.SaveAs Password:="Password"

That solved the problem.

Sub Splitbooka()
'Updateby20140612
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "" & xWs.Name & ".xlsx"
ActiveWorkbook.SaveAs Password:="Password"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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