"Protect Sheet"

E

Edward

I am using Excel 2007 with Vista Home Premium.

I have 15 workbooks, each with 12 sheets. I want each sheet (not the
workbook) password protected to prevent any accidental to interference with
formulae.

With a total of 180 worksheets I am a bit disappointed there is no way other
than protecting each sheet one at a time entering each password twice...!
......Or am I wrong? I sincerely hope I am.

Can anyone help?

TIA
 
D

Dave Peterson

You didn't say how those 15 workbooks could be found, but you could look at Ron
de Bruin's site to see how he loops through files in a folder.

http://www.rondebruin.nl/copy3.htm (for Dir())
and
http://www.rondebruin.nl/fso.htm (for FSO)

But if you wanted to open the workbooks manually, you could run a macro that
protects each of the worksheets in the activeworkbook.

I saved this from a previous post:

Maybe you can create a top secret macro that unprotects all the worksheets in
the activeworkbook and then another macro that protects all those sheets.

Start a new workbook
hit alt-f11 (to get to the VBE where macros live)
Hit F4 (to see the project explorer--kind of like windows explorer)
select your project
Insert|Module
Paste this into the newly opened code window:

Option Explicit
Sub UnprotectAll()
Dim wks As Worksheet
Dim pwd As String
pwd = InputBox(Prompt:="What's the password, Kenny?")

If Trim(pwd) = "" Then
Exit Sub
End If

For Each wks In ActiveWorkbook.Worksheets
With wks
If .ProtectContents = True _
Or .ProtectDrawingObjects = True _
Or .ProtectScenarios = True Then
On Error Resume Next
.Unprotect Password:=pwd
If Err.Number <> 0 Then
MsgBox "Something went wrong with: " & wks.Name
Err.Clear
'exit for 'stop trying???
End If
On Error GoTo 0
End If
End With
Next wks

End Sub
Sub ProtectAll()
Dim wks As Worksheet
Dim pwd As String
pwd = InputBox(Prompt:="What's the password, Kenny?")

If Trim(pwd) = "" Then
Exit Sub
End If

For Each wks In ActiveWorkbook.Worksheets
With wks
If .ProtectContents = True _
Or .ProtectDrawingObjects = True _
Or .ProtectScenarios = True Then
'do nothing, it's already protected
Else
On Error Resume Next
.Protect Password:=pwd
If Err.Number <> 0 Then
MsgBox "Something went wrong with: " & wks.Name
Err.Clear
'exit for 'stop trying???
End If
On Error GoTo 0
End If
End With
Next wks
End Sub

Then back to excel and save this workbook with a nice name.

Anytime you want to unprotect or protect all the worksheets in any workbook, you
can open this file.

Then activate the workbook that you want to make changes to.
Hit alt-f8
Select the macro
and click Run

If you really wanted, you could embed the password directly in the code (both
procedures) and not be bothered with a prompt.

Change this line:

pwd = InputBox(Prompt:="What's the password, Kenny?")
to
pwd = "TopSecretPaSsWord1234_x"


If you're new to macros:

Debra Dalgleish has some notes how to implement macros here:
http://www.contextures.com/xlvba01.html

David McRitchie has an intro to macros:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Ron de Bruin's intro to macros:
http://www.rondebruin.nl/code.htm

(General, Regular and Standard modules all describe the same thing.)
 
C

CurlyDave

I am using Excel 2007 with Vista Home Premium.

I have 15 workbooks, each with 12 sheets. I want each sheet (not the
workbook) password protected to prevent any accidental to interference with
formulae.

With a total of 180 worksheets I am a bit disappointed there is no way other
than protecting each sheet one at a time entering each password twice...!
.....Or am I wrong? I sincerely hope I am.

Can anyone help?

TIA

Try this worksheet loop,

Sub ProtectAllSheets()
Dim Sh As Worksheet

For Each Sh In ThisWorkbook.Sheets
Sh.Protect Password:="123"
Next

End Sub

Sub UnProtectAllSheets()
Dim Sh As Worksheet

For Each Sh In ThisWorkbook.Sheets
Sh.Unprotect Password:="123"
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top