Help with VB Script

S

Storm

Hello.

I have a Macro (VB script) that will help me protect all worksheets within a
workbook. It prompts for the password to protect all sheets when the Macro
is run.

Can someone help me revise the code below where only 2 worksheets are
protected out of a 3-worksheet workbook? Thank you!

Sub Protect()
Dim myPWD As String
Dim wks As Worksheet
myPWD = Application.InputBox("Enter Password: ")
For Each wks In ActiveWorkbook.Worksheets
wks.Protect Password:=myPWD
Next wks
End Sub
 
G

Gary''s Student

Sub Protect()
Dim myPWD As String
Dim wks As Worksheet
myPWD = Application.InputBox("Enter Password: ")
i = 0
For Each wks In ActiveWorkbook.Worksheets
wks.Protect Password:=myPWD
i = i + 1
If i = 2 Then Exit Sub
Next wks
End Sub
 
J

JW

replace "the one you want to exclude" with whatever the name of the
sheet is that you want to exclude.

Sub Protect()
Dim myPWD As String
Dim wks As Worksheet
myPWD = Application.InputBox("Enter Password: ")
For Each wks In ActiveWorkbook.Worksheets
If wks.Name <> "the one you want to exclude" Then _
wks.Protect Password:=myPWD
Next wks
End Sub
 
S

Storm

Thank you!

JW said:
replace "the one you want to exclude" with whatever the name of the
sheet is that you want to exclude.

Sub Protect()
Dim myPWD As String
Dim wks As Worksheet
myPWD = Application.InputBox("Enter Password: ")
For Each wks In ActiveWorkbook.Worksheets
If wks.Name <> "the one you want to exclude" Then _
wks.Protect Password:=myPWD
Next wks
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