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
 
I have to give a HUGE SHOUT OUT to CharlesH and Michael M....... THANK YOU! THANK YOU! THANK YOU!

Y'all gave me the advice and the tools to learn, which is an amazing thing!

The code that finally worked is:

Code:
Sub BLM_RENAME_SHEET()
Dim WS As Worksheet
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect ("blm_rental")
For Each WS In ActiveWindow.SelectedSheets
    WS.Activate
        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
ActiveWorkbook.Protect ("blm_rental")
Next
End Sub
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,216,119
Messages
6,128,944
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