Solved

Re-protecting a spreadsheet Macro

Posted on 2011-09-05
11
313 Views
Last Modified: 2012-08-14

I have a worksheet that I have protected.  I periodically give users the password in order for them to make changes.  When they click on one of the buttons on the worksheet, I want the spreadsheet to password enable the spreadsheet again.

Sub Reactivate_Password()
    ActiveSheet.Protect Password:="password"
End Sub

I have this macro embedded in the button that takes a user back to another worksheet and that's where I wanted to call this macro.

I thought this to be a simple one.

Thank you in advance.

B.
0
Comment
Question by:Bright01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

by:TinTombStone
ID: 36485959
Sub Button_Code

'Stuff here

Reactivate_Password
End Sub
0
 

Author Comment

by:Bright01
ID: 36486011
TinTombStone,

I think that's what I've got.

Sub ReturntoQuestionaire_Click()
'This closes the Existing sheet and returns the user to the Questionaire
Sheet42.Visible = xlSheetHidden
Reactivate_Password
Sheet1.Activate
End Sub

It doesn't work however.  What am I missing?

B.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36486077
Where is this module located?  The ReturnToQuestionnaire really should be in a public module, as well as the Reactivate_Password code, and the button_click code.  All should be public.  If you have them embedded in a sheet's codepage, in different places (a sheet's codepage is visible only to itself, but it can reference public routines).  Perhaps that's the problem.

What line do you get the error?

Here's a sample of your code in a workbook to play this out.  Let me know if it works for you.  See where I placed the code, and see if you did something similar.

Hope this helps!

Dave
resetPassword-r1.xlsm
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Bright01
ID: 36486172
Dave,

Greetings!  The Reactivate_Password sub is in a module.  

Sub Reactivate_Password()
    ActiveSheet.Protect Password:="pass"
'    ActiveSheet.EnableSelection = xlUnlockedCells
End Sub

However, the sub is called from a worksheet sub.

Here's what's in the Worksheet.

Sub ReturntoQuestionaire_Click()
'This closes the Maturity by Category and returns the user to the Questionaire
Sheet42.Visible = xlSheetHidden
Reactivate_Password
Sheet1.Activate
End Sub
0
 
LVL 42

Assisted Solution

by:dlmille
dlmille earned 150 total points
ID: 36486223
Try putting the word PUBLIC before the word sub on the reactivate:

Public Sub Reactivate_Password()

Also, its good practice to use CALL in front of procedure calls, so change the line in the ReturnToQuestionaire_Click() to:

Call Reactivate_Password.

I also suggest moving this all to one public module, as in the example I posted.  You don't need mouse click routines in worksheet code pages.  While it may work, it is not good practice to put more code than just event management in worksheet code pages.  Results may not always be guaranteed.

Again, however, I ask, "What line did you get the error?".  Or are you getting any errors?  What are you experiencing?

Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 36486226
Also, can you clear your workbook of any confidential data and just upload it?  I can assist more readily, then.

Dave
0
 

Author Comment

by:Bright01
ID: 36486264
Dave,

Much thanks for stepping in here.

The spreadsheet works great except when I press the "Return to Questionaire" button, it doesn't relock the sheet.  The code in the specific worksheet is;

Sub ReturntoQuestionaire_Click()
Sheet42.Visible = xlSheetHidden
Call Reactivate_Password
Sheet1.Activate
End Sub

This button returns the user to the Questionaire.

The Module that has the subroutine looks like this;

Public Sub Reactivate_Password()
    ActiveSheet.Protect Password:="pass"
'    ActiveSheet.EnableSelection = xlUnlockedCells
End Sub

I added Pulblic as you suggested and also added "call" to the routine in the spreadsheet.  What's odd is that when I use the button/sub on the worksheet, it does not activate the password or protect.

B.

0
 
LVL 42

Expert Comment

by:dlmille
ID: 36486285
can you upload?
0
 

Author Comment

by:Bright01
ID: 36487441
Dave,

Here you go.  It's a mocked up version but I'm certain you will see what I'm trying to do immediately.  I'm simply trying to relock the worksheet (target) when you return to the other (source) worksheet.

Thank you,

B.
Relocking-Spreadsheet.xlsm
0
 
LVL 6

Accepted Solution

by:
TinTombStone earned 350 total points
ID: 36488825

In Sub ReturntoSource_Click()


swap the lines

Sheet2.Visible...
and
Call...


This should work

Sub ReturntoSource_Click()
'This closes the Target and returns to Source
Call Reactivate_Password
Sheet2.Visible = xlSheetHidden
Sheet1.Activate
End Sub
0
 

Author Closing Comment

by:Bright01
ID: 36489429
Thank you TinTombStone for providing the code.  It does the job.

Thank you Dave for the comments and lesson on what I should be doing with a global macro.

Great teamwork you guys,

B.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
EXCEL file checking. 11 45
Cannot locate cell 15 37
sort time order 10 39
Custom fill series 12 30
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question