Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 219
Default Protecting Specific Cells In Excel

Here's some code with a lot of commenting. Read through it and I think
you'll get the gist of what you need.

'/=========================================/
Public Sub Protect_Unprotect()
'Starting with the assumption that the worksheet is
' protected, you'll need to get information on HOW it
' is protected so declare variables for these 3 pieces
' of information
'The contents, Drawing Objects and/or Scenarios can all
' be protected OR NOT in the worksheet
Dim blnProtectContents As Boolean
Dim blnProtectDrawingObjects As Boolean
Dim blnProtectScenarios As Boolean

'if there is a password, you'll need to get that
' so declare a variable for the password
Dim strPassword As String

'set default for whether worksheet is protected or not
blnProtectContents = False
blnProtectDrawingObjects = False
blnProtectScenarios = False

'check if worksheet unprotected
' if it's protected, get various information
On Error Resume Next
If Application.ActiveSheet.ProtectContents = True Then
blnProtectContents = True
If Application.ActiveSheet.ProtectDrawingObjects = True Then
blnProtectDrawingObjects = True
End If
If Application.ActiveSheet.ProtectScenarios = True Then
blnProtectScenarios = True
End If
'try unprotecting the worksheet without a password
ActiveSheet.Unprotect
'check if worksheet is still protected
If Application.ActiveSheet.ProtectContents = True Then
'still protected so try password
strPassword = InputBox("Enter Password: " & vbCr & vbCr & _
"If there is no password, press ENTER." & vbCr & vbCr & _
"ONLY enter Password if source of this macro is TRUSTED!!!", _
"Password to Unprotect Worksheet...", "")
ActiveSheet.Unprotect Password:=strPassword
'check if worksheet is still protected
'if password didn't work - still not unprotected so stop process
If Application.ActiveSheet.ProtectContents = True Then
Exit Sub
End If
End If
End If
On Error GoTo 0

'now that worksheet is unprotected, set the protection
' property of the cells that you selected
Selection.Locked = True
' or unprotect the cells that you selected
Selection.Locked = False

'comment out one of the two "Selection.Locked = " lines above
' depending on what you want to accomplish

'set worksheet back to original protected/unprotected state
On Error Resume Next
ActiveSheet.Protect Password:=strPassword, _
DrawingObjects:=blnProtectDrawingObjects, _
Contents:=blnProtectContents, Scenarios:=blnProtectScenarios

End Sub
'/=========================================/


HTH,
--
Gary Brown

If this post was helpful, please click the ''''Yes'''' button next to
''''Was this Post Helpfull to you?".


"excel_slave - ExcelForums.com" wrote:

Hi!

I am trying to protect specific cells in Excel, and would like to get
a few lines of VBA code for the same. I have tried changing settings
(Protection) for the worksheet and then using the Range().Protected =
True/False combinations, but have not been able to make it to work.
I'd appreciate if someone could write the process in a step-by-step
manner.

Thanks!


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
protecting specific columns in a worksheet Polochilde Excel Discussion (Misc queries) 3 July 7th 08 06:13 AM
Protecting specific columns within a worksheet Deb Excel Worksheet Functions 1 June 24th 08 04:23 PM
Protecting a Range of Specific Cell/s Blacksmith[_2_] Excel Discussion (Misc queries) 1 August 27th 07 04:43 PM
protecting specific parts of my worksheet. Yaka New Users to Excel 2 June 20th 07 01:28 AM
Protecting specific cells in a worksheet. GaryS Excel Worksheet Functions 2 January 6th 05 04:47 PM


All times are GMT +1. The time now is 11:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"