Code to UNPROTECT then PROTECT my WORKBOOK when Macro runs - Can someone show me please???

Barryoffshore

Board Regular
Joined
Feb 21, 2005
Messages
73
Okay, I have this code and it works very well - but it only works if my WORKBOOK is unprotected. I know (thanks to this forum and ALL the GREAT people here) how to Unprotect then Protect an ActiveSheet, but I can't figure out how to Unprotect then Protect my workbook when the macro runs... Can someone please show me what I need to do?

Thanks!!! Bear

Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Application.ScreenUpdating = False
For Each WS In ActiveWindow.SelectedSheets
WS.Activate
A1 = WS.Range("C2").Value
If A1 <> "" Then
If InStr(C2, " - ") Then
s = Split(Trim(E8), " - ")
WS.Name = s(0)
Else
WS.Name = WS.Range("C2").Value
End If
End If
Next
End Sub
 
HI,

Where does the code stop?
Also see if this works.

Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Dim myPassword As String
myPassword = "password"''''' Be sure you change this to your actual password.
Application.ScreenUpdating = False
For Each WS In thisworkbook.worksheets
    WS.Activate
    WS.Unprotect Password:=myPassword
    A1 = WS.Range("C2").Value
    If A1 <> "" Then
        If InStr(C2, " - ") Then
            s = Split(Trim(E8), " - ")
            WS.Name = s(0)
        Else
            WS.Name = WS.Range("C2").Value
        End If
    End If
    WS.Protect Password:=myPassword
Next
End Sub
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi,

Amended code so that it will not look at "Retail" and "Summary" sheets.

Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Dim myPassword As String
myPassword = "password" ''''' Be sure you change this to your actual password.
Application.ScreenUpdating = False
For Each WS In ThisWorkbook.Worksheets
    If WS.Name <> "Rental Sheet Inst" And WS.Name <> "Summary Sheet" Then
        WS.Activate
        WS.Unprotect Password:=myPassword
        A1 = WS.Range("C2").Value
        If A1 <> "" Then
            If InStr(C2, " - ") Then
                s = Split(Trim(E8), " - ")
                WS.Name = s(0)
            Else
                WS.Name = WS.Range("C2").Value
            End If
        End If
        WS.Protect Password:=myPassword
    End If
Next
End Sub
 
Upvote 0
CharlesH - Thank you again and a thousand times over for all your help - I keep getting an error (see below) and I've tried everything I can think of to fix it....... but as always, I can't.

(and i'm not worried about the password being seen here - I'll change it when we're done)

Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Dim myPassword As String
Application.ScreenUpdating = False
myPassword = "blm_rental"
For Each WS In ThisWorkbook.Worksheets
If WS.Name <> "Rental Sheet Inst" And WS.Name <> "Summary Sheet" Then
WS.Activate
WS.Unprotect Password:=myPassword
A1 = WS.Range("C2").Value
If A1 <> "" Then
If InStr(C2, " - ") Then
s = Split(Trim(E8), " - ")
WS.Name = s(0)
Else
WS.Name = WS.Range("C2").Value
End If
End If
WS.Protect Password:=myPassword
Next ----------------------------------------------------- Right here I get a "Compile Error - Next Without For"
End Sub
 
Upvote 0
Bear
If you learn to indent your code you'll see that your missing an End If, before the ws.protect line
Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Dim myPassword As String
Application.ScreenUpdating = False
myPassword = "blm_rental"
    For Each WS In ThisWorkbook.Worksheets
        If WS.Name <> "Rental Sheet Inst" And WS.Name <> "Summary Sheet" Then
            WS.Activate
            WS.Unprotect Password:=myPassword
            A1 = WS.Range("C2").Value
                If A1 <> "" Then
                    If InStr(C2, " - ") Then
                        s = Split(Trim(E8), " - ")
                        WS.Name = s(0)
                        Else
                        WS.Name = WS.Range("C2").Value
                End If
        End If
    WS.Protect Password:=myPassword
    Next '----------------------------------------------------- Right here I get a "Compile Error - Next Without For"
End Sub
 
Upvote 0
Micheal M - Thank you for your input and your help!!! I can always learn from people like you, CharlesH.

HOWEVER, this code gave me the same result - it says "Compile error in hidden module2. This error commonly occurs when code is incompatible with the version, platform, or architecture of this application."

If it helps, I am using Excel 2010

Thanks again for all you and everyone has done for me and to help me.

Bear

Bear
If you learn to indent your code you'll see that your missing an End If, before the ws.protect line
Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Dim myPassword As String
Application.ScreenUpdating = False
myPassword = "blm_rental"
    For Each WS In ThisWorkbook.Worksheets
        If WS.Name <> "Rental Sheet Inst" And WS.Name <> "Summary Sheet" Then
            WS.Activate
            WS.Unprotect Password:=myPassword
            A1 = WS.Range("C2").Value
                If A1 <> "" Then
                    If InStr(C2, " - ") Then
                        s = Split(Trim(E8), " - ")
                        WS.Name = s(0)
                        Else
                        WS.Name = WS.Range("C2").Value
                End If
        End If
    WS.Protect Password:=myPassword
    Next '----------------------------------------------------- Right here I get a "Compile Error - Next Without For"
End Sub
 
Upvote 0
Have you got a Sheet Change or Selection Change event macro that gets activated when this code is run ??
If so, that could be causing the problem
Try running the code manually, and see what triggers the Sheet module !
 
Upvote 0
HI,

Part of the code is missing.
Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Dim myPassword As String
Application.ScreenUpdating = False
myPassword = "blm_rental"
    For Each WS In ThisWorkbook.Worksheets
        If WS.Name <> "Rental Sheet Inst" And WS.Name <> "Summary Sheet" Then
            WS.Activate
            WS.Unprotect Password:=myPassword
            A1 = WS.Range("C2").Value
                If A1 <> "" Then
                    If InStr(C2, " - ") Then
                        s = Split(Trim(E8), " - ")
                        WS.Name = s(0)
                        Else
                        WS.Name = WS.Range("C2").Value
                      end if''''''  ADDED
                End If
        End If
    WS.Protect Password:=myPassword
    Next '----------------------------------------------------- Right here I get a "Compile Error - Next Without For"
End Sub
 
Upvote 0
Hi,

Along with Michael M. You should indent your code. It will be easier to read and trouble shoot.

Also if you have the code in the "Worksheet" code module. You really should place it in a regular code module.
If you have code in "ThisWorkbook" module. I think you should only have code in it that you want to run when the workbook opens.
All other code should be in a regular module.
 
Upvote 0
This code works to change the SheetName if the Workbook is unprotected. However, if the Workbook is protected I get the Run-time error ‘1004’ and it shows the bug is in the "WS.Name = WS.Range("C2").Value" line.

Also, if I run it without the sheet being protected, or the workbook being protected – it works to change the name on the active sheet only, but it goes through and protects all the sheets in the workbook, but not the workbook itself (which unprotecting the Workbook, changing the sheetname [in the active sheet] to the value of cell C2, then protecting the Workbook is what I’m needing it to do).

I’m absolutely stumped here…

Charles and Michael M, I can’t thank y'all enough for all you have done for me!!!

Bear

Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Dim myPassword As String
Application.ScreenUpdating = False
myPassword = "blm_rental"
    For Each WS In ThisWorkbook.Worksheets
        If WS.Name <> "Rental Sheet Inst" And WS.Name <> "Summary Sheet" Then
            WS.Activate
            WS.Unprotect Password:=myPassword
            A1 = WS.Range("C2").Value
                If A1 <> "" Then
                    If InStr(C2, " - ") Then
                        s = Split(Trim(C2), " - ") 
                        WS.Name = s(0)
                        Else
                        WS.Name = WS.Range("C2").Value
                      End If
                End If
        End If
    WS.Protect Password:=myPassword
    Next
End Sub
 
Last edited:
Upvote 0
I can't see anywhere in the code where you are protecting the Workbook..???
you'll need to do that to make it do as required

Code:
    ActiveWorkbook.Protect Structure:=True, Windows:=True
 
Upvote 0

Forum statistics

Threads
1,216,117
Messages
6,128,935
Members
449,480
Latest member
yesitisasport

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