Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4
Default Code to protect/unprotect a sheet using a macro with password

I have a worksheet that I created that requires a lot of sorting and other
macros. However with the worksheet protected I can't perform the functions. I
need the worksheet password protected so the user can't mess up the formulas.
Therefore, I need to un-protect the worksheet, sort the rows and then
re-protect the worksheet. I can do it if I give the user the password but
then they can just un-protect it. Therefore I want to have the password in
the macro so it is done automatically. Help!
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 709
Default Code to protect/unprotect a sheet using a macro with password

FredH, something like this,

Const PW As String = "123" 'Change Password Here

ActiveSheet.Unprotect Password:=PW

'you code here

ActiveSheet.Protect Password:=PW



And protect the VBA project so they can't see the password there, like this

To protect the VBA project, from your workbook right-click the workbook's
icon and pick View Code. This icon is at the top-left of the spreadsheet
this will open the VBA editor, in Project Explorer right click on your
workbook name, if you don't see it press CTRL + r to open the Project
Explorer then select VBA project properties, protection, check lock project
for viewing and set a password. Press Alt and Q to close this window and go
back to your workbook and save and close the file. Be aware that this
password can be broken by third party software

"FredH" wrote in message
...
I have a worksheet that I created that requires a lot of sorting and other
macros. However with the worksheet protected I can't perform the
functions. I
need the worksheet password protected so the user can't mess up the
formulas.
Therefore, I need to un-protect the worksheet, sort the rows and then
re-protect the worksheet. I can do it if I give the user the password but
then they can just un-protect it. Therefore I want to have the password in
the macro so it is done automatically. Help!



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4
Default Code to protect/unprotect a sheet using a macro with password



"Paul B" wrote:

FredH, something like this,

Const PW As String = "123" 'Change Password Here

ActiveSheet.Unprotect Password:=PW

'you code here

ActiveSheet.Protect Password:=PW



And protect the VBA project so they can't see the password there, like this

To protect the VBA project, from your workbook right-click the workbook's
icon and pick View Code. This icon is at the top-left of the spreadsheet
this will open the VBA editor, in Project Explorer right click on your
workbook name, if you don't see it press CTRL + r to open the Project
Explorer then select VBA project properties, protection, check lock project
for viewing and set a password. Press Alt and Q to close this window and go
back to your workbook and save and close the file. Be aware that this
password can be broken by third party software

"FredH" wrote in message
...
I have a worksheet that I created that requires a lot of sorting and other
macros. However with the worksheet protected I can't perform the
functions. I
need the worksheet password protected so the user can't mess up the
formulas.
Therefore, I need to un-protect the worksheet, sort the rows and then
re-protect the worksheet. I can do it if I give the user the password but
then they can just un-protect it. Therefore I want to have the password in
the macro so it is done automatically. Help!


The following are my two macros. I don't have any sort codes in them. The protect macro puts the password test in but the unprotect macro doesn't work.

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4
Default Code to protect/unprotect a sheet using a macro with password



"Paul B" wrote:

FredH, something like this,

Const PW As String = "123" 'Change Password Here

ActiveSheet.Unprotect Password:=PW

'you code here

ActiveSheet.Protect Password:=PW



And protect the VBA project so they can't see the password there, like this

To protect the VBA project, from your workbook right-click the workbook's
icon and pick View Code. This icon is at the top-left of the spreadsheet
this will open the VBA editor, in Project Explorer right click on your
workbook name, if you don't see it press CTRL + r to open the Project
Explorer then select VBA project properties, protection, check lock project
for viewing and set a password. Press Alt and Q to close this window and go
back to your workbook and save and close the file. Be aware that this
password can be broken by third party software

"FredH" wrote in message
...
I have a worksheet that I created that requires a lot of sorting and other
macros. However with the worksheet protected I can't perform the
functions. I
need the worksheet password protected so the user can't mess up the
formulas.
Therefore, I need to un-protect the worksheet, sort the rows and then
re-protect the worksheet. I can do it if I give the user the password but
then they can just un-protect it. Therefore I want to have the password in
the macro so it is done automatically. Help!


Below are the macros i use. The protect macro works but the upprotect doesn'tSub Unprotect()

'
' Unprotect Macro
' Macro recorded 4/6/2007 by Charles Hord
'
' Keyboard Shortcut: Ctrl+u
'
ActiveSheet.Unprotect Password:="test"
Range("A2").Select
Application.Run "'Macro test.xls'!Protect"


End SubSub Protect()
'
' Protect Macro
' Protects sheet password is test
'

'
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True, Password:="test"

End Sub



  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 21
Default Code to protect/unprotect a sheet using a macro with password

It wasn't totally clear from the above but I had the same issue where the sub
unprotect wouldn't work.

I realised it was because PW was only defined within "sub protect" but not
within "sub unprotect".

Fixed easily by defining PW within both subroutines.


  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4,624
Default Code to protect/unprotect a sheet using a macro with password

One way:

Const csPWORD As String = "drowssap"
ActiveWorkbook.Worksheets("Sheet1").Unprotect Password:=csPWORD
'Sort
ActiveWorkbook.Worksheets("Sheet1").Protect Password:=csPWORD




In article ,
FredH wrote:

I have a worksheet that I created that requires a lot of sorting and other
macros. However with the worksheet protected I can't perform the functions. I
need the worksheet password protected so the user can't mess up the formulas.
Therefore, I need to un-protect the worksheet, sort the rows and then
re-protect the worksheet. I can do it if I give the user the password but
then they can just un-protect it. Therefore I want to have the password in
the macro so it is done automatically. Help!

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
Macro to Protect OK, Unprotect now messed up! Stilla Excel Worksheet Functions 12 January 7th 09 02:22 PM
protect / unprotect VBA project by macro sylvain Excel Discussion (Misc queries) 0 July 31st 06 06:09 PM
Forgot the password (to unprotect a sheet) Venkatesh V Excel Discussion (Misc queries) 1 March 29th 06 08:32 AM
is there anyway to make it so the users of my excel spread sheet cant view the macro code w/o a password? Daniel Excel Worksheet Functions 2 June 28th 05 05:34 AM
password protect vba code greg Excel Discussion (Misc queries) 3 December 8th 04 04:03 PM


All times are GMT +1. The time now is 08:36 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"