Can I lock every sheet?

Tarqs

Board Regular
Joined
Feb 14, 2011
Messages
120
I'm using the below code but i need to lock every new sheet that it creates?

If you could help it would be much appreciated.

Code:
Sub ExplodeTotal()
        ActiveSheet.PivotTables("PivotTable1").ShowPages PageField:="Supplier"
        Application.ScreenUpdating = False
End Sub

Tarqs
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This is untested, but try:

Code:
Sub ExplodeTotal()
Dim shtcnt  As Long, _
    i       As Long
    
shtcnt = Sheets.Count
Application.ScreenUpdating = False

ActiveSheet.PivotTables("PivotTable1").ShowPages PageField:="Supplier"

For i = shtcnt + 1 To Sheets.Count
    Sheets(i).Protect
Next i

Application.ScreenUpdating = True
End Sub
 
Upvote 0
MrKowz,

Thanks for taking the time.

Nearly works, but two things.

1. It locks all but the first sheet which is created.
2. Can I put a password into the code as currently clicking the unlock button from the ribbon will defeat it?

Tarqs
 
Upvote 0
Try:

Code:
Sub ExplodeTotal()
Dim shtcnt  As Long, _
    i       As Long
    
shtcnt = Sheets.Count
Application.ScreenUpdating = False
ActiveSheet.PivotTables("PivotTable1").ShowPages PageField:="Supplier"
For i = [COLOR=red][B]shtcnt[/B][/COLOR] To Sheets.Count
    Sheets(i).Protect [COLOR=red][B]Password:=[/B][/COLOR][B][COLOR=red]"abc123[/COLOR][COLOR=red]"
[/B][/COLOR]Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Mr Kowz,

Apologies but time to be cheeky! Now I have locked the outputs i need to resize the columns or you can't see all the data.

Do you no how to add the below into the code?

Code:
ws.Cells.EntireColumn.AutoFit

Tarqs
 
Upvote 0
Try:

Code:
Sub ExplodeTotal()
Dim shtcnt  As Long, _
    i       As Long
    
shtcnt = Sheets.Count
Application.ScreenUpdating = False
ActiveSheet.PivotTables("PivotTable1").ShowPages PageField:="Supplier"
For i = shtcnt To Sheets.Count
    Sheets(i).Cells.EntireColumn.AutoFit
    Sheets(i).Protect Password:="abc123"
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

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