You are here:

Excel/protect and unprotect sheet

Advertisement


Question
Hi Aiden

I have recorded the following macro:

Sub insertformatting()
'
' insertformatting Macro
'

'
   Range( _
       "A1,B3,E3,B4:G4,I3,I4:O4,L3,B7:G7,I7:O7,B10:G10,I10:O10,B16,E16,B17:G17,I16,I17:O17,L16,B20:G20,I20:O20,B23:G23,I23:O23" _
       ).Select
   Range("I23").Activate
   With Selection.Interior
       .PatternColorIndex = xlAutomatic
       .ThemeColor = xlThemeColorAccent3
       .TintAndShade = 0.399945066682943
       .PatternTintAndShade = 0
   End With
   Range("A1").Select
   With Selection.Interior
       .Pattern = xlNone
       .TintAndShade = 0
       .PatternTintAndShade = 0
   End With
End Sub


I wish to add a line at the beginning that will unprotect the active sheet and add a line at the end that will protect the active sheet.  Is this something that you can help me with?  

Thanks in advance.

Chris Mitchell

Answer
I'd amend the recorded macro to NOT select the range and simply apply the formatting, but that's just me!  

ActiveSheet.Protect Password = "test"
ActiveSheet.Unprotect Password = "test"

are the lines you need - either remove the password bit entirely if you don't want a password or amend my "test" to whatever you need it to be.  My full version of your macro would be

ActiveSheet.Unprotect Password = "test"

  With Range("B3,E3,B4:G4,I3,I4:O4,L3,B7:G7,I7:O7,B10:G10,I10:O10,B16,E16,B17:G17,I16,I17:O17,L16,B20:G20,I20:O20,B23:G23,I23:O23").Interior
      .PatternColorIndex = xlAutomatic
      .ThemeColor = xlThemeColorAccent3
      .TintAndShade = 0.399945066682943
      .PatternTintAndShade = 0
  End With
ActiveSheet.Protect Password = "test"


(I've assumed that A1 would be OK if we didn't include it in the first selection)
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Aidan Heritage

Expertise

I have provided first hand support since `95 for Microsoft Office majoring in Word and Excel - support for all versions from 2 onwards. I'm based in the UK, so please allow for time differences when asking me questions from other parts of the world!

Experience

My background is in the insurance industry and call centre areas, but have been called upon to provide many varied solutions.

Education/Credentials
I'm educated to UK A level standard, but as I left school some 30 years ago that is rather irrelevent - university of life has provided more of a background!

©2016 About.com. All rights reserved.