Google Answers Logo
View Question
 
Q: How to perform sorting, find&replace on a password protected MS Excel worksheet ( No Answer,   3 Comments )
Question  
Subject: How to perform sorting, find&replace on a password protected MS Excel worksheet
Category: Computers > Programming
Asked by: jgolfer-ga
List Price: $10.00
Posted: 21 Sep 2004 12:42 PDT
Expires: 21 Oct 2004 12:42 PDT
Question ID: 404294
I created an Excel worksheet template with password protection. Some
cells are modifiable while others are not ( I did this by unlocking those
modifiable cells before applying password protection to the
worksheet). Because of the worksheet is password protected, people who
use my template are now unable to,

1) Sort by column
2) Find and Replace
3) Adjust column width

How to solve the problem without having to unprotect the worksheet?

Clarification of Question by jgolfer-ga on 22 Sep 2004 09:45 PDT
dreamboat-ga and aeh5a2-ga, thanks a lot for your comments. I haven't
got a chance to try your VB script. As far as the Find & Replace is
concerned, it needs to be able to Find and Replace (or Replace All)
for the whole worksheet but ONLY within the modifiable cells, not the
protected cells.
Answer  
There is no answer at this time.

Comments  
Subject: Re: How to perform sorting, find&replace on a password protected MS Excel worksheet
From: aeh5a2-ga on 21 Sep 2004 21:15 PDT
 
First, choose the cells that you want the individuals to be able to
edit.  Select them one at a time or all at once (by holding control
and selecting the cells or columns).  If you then right click you can
"format cells".  Under cell formating all you need to do is click on
protection tab and then the Lock cell marker needs to be checked off. 
Once this is done these cells/cloumns will be able to be edited.  I do
this all the time for the company I work for and now how big of a pain
life would be without it.
Subject: Re: How to perform sorting, find&replace on a password protected MS Excel worksheet
From: dreamboat-ga on 21 Sep 2004 23:58 PDT
 
I am getting you some VBA macro code and instructions to put into your
workbook. It will build a special toolbar for you and, when they click
SORT (for instance) on this toolbar, it will unprotect, sort, then
reprotect the worksheet. A problem however is that in order to do this
properly would require all kinds of error handling code, etc. I assume
you are unfamiliar with VBA. There is a wealth of information and
sample code on the internet that can help you get exactly what you
need.

As for the find and replace, here is an excellent example of a macro
that runs a find and replace in Excel:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=201

You may want THAT type of find and replace as opposed to the kind that
requires entry of spaces to differentiate, particularly when cleaning
up addresses.

Anyway, I should have some code and instructions for you to do a
standard sort, and standard find and replace within about 24 hours.
Subject: Re: How to perform sorting, find&replace on a password protected MS Excel worksheet
From: dreamboat-ga on 22 Sep 2004 00:57 PDT
 
Okay, here we go.

Make a copy of your workbook. I would hate for this not to work right
and we mess up your original.

Now, open the copy.
Hit Alt+F11 to get to the Visual Basic Editor (VBE).

On the left-hand side of the window, double-click ThisWorkbook.
On the right-hand side, paste the following code:

Start code----------------------------

Private Sub Workbook_BeforeClose(Cancel As Boolean)
     'This closes the toolbar when the workbook is closed.. This is
what we call an *on-event* procedure (macro) because it is
     'run when the workbook is closed.
    On Error Resume Next
    Application.CommandBars("Worksheet Menu Bar").Controls("My&Menu").Delete
End Sub

Private Sub Workbook_Open()
'The Dim statements make the rest of the code easier to create.
Dim Mybar As CommandBarControl
Dim myButton As CommandBarControl
Dim i As Integer
Dim A(2) As Variant
     
    On Error Resume Next
     'This checks if the menu already exists. If it does, it does not
create a new one.
     'The ampersand (&) in the name of the menu underlines the letter
that follows it to give
     'it a keyboard command (Alt-m) as many menus have.
     
    Application.CommandBars("Worksheet Menu Bar").Controls("My&Menu").Delete
    
    
    With Application.CommandBars("Worksheet Menu Bar").Controls
        Set Mybar = .Add(Type:=msoControlPopup, Before:=11)
        Mybar.Caption = "My&Menu"
    End With
    
         'Note that the parts of the array are (  "  Title of menu
option  "  ,  "  Macro to Run  "  , FaceID for toolbar button)
        A(1) = Array("Autofit", "AutofitAll", 80)
        A(2) = Array("Sort B", "SortB", 81)
        For i = 1 To UBound(A)
            With Mybar.Controls
                Set myButton = .Add(Type:=msoControlButton)
                With myButton
                    .Caption = A(i)(0)
                    .OnAction = A(i)(1)
                    .FaceId = A(i)(2)
                End With
            End With
        Next i
End Sub

End code-------------------------

In the code above, change My&Menu to any menu name you like.
You can get a very cool little tool from John Walkenbach's site, which
provides the Face IDs for command buttons (or just use those I've
provided):
http://j-walk.com/ss/excel/tips/tip67.htm

Now, hit Insert-Module from the menu, and paste the following code:

Start code-----------------

Sub AutoFitAll()
'autofits all columns in a worksheet
     
    ActiveSheet.Unprotect passwordhere
          
    Application.ScreenUpdating = False
    Dim wkSt As String
    Dim wkBk As Worksheet
    wkSt = ActiveSheet.Name
    For Each wkBk In ActiveWorkbook.Worksheets
        On Error Resume Next
        wkBk.Activate
        Cells.EntireColumn.AutoFit
    Next wkBk
    Sheets(wkSt).Select
    Application.ScreenUpdating = True
     
    ActiveSheet.Protect passwordhere
    
End Sub

Sub SortB()
'sorts column B
     
    ActiveSheet.Unprotect passwordhere

        Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    ActiveSheet.Protect passwordhere
    
End Sub


End Code---------------------------------

Change B1 in the code above to the cell that would be active when
hitting the sort button. If you have multiple columns by which you
want to allow sorting, then you'll have to provide multiple macros, or
we'd have to set up a userform.

Change "passwordhere" to your password, if any. If no password, simply
delete the whole word "passwordhere" in both lines of code above.


To do your find and replace, I really need more information. Will we
be searching in entire cells? In other words, does the entire cell
have to match the FIND criteria? Or will we be looking for words
within cells too? Please give as much information as you can about
your need for find and replace.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy