Solved

Using VBA to Hide and Lock Cells with a password

Posted on 2014-10-06
4
195 Views
Last Modified: 2014-10-06
Hello,

I opened another question here:

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28531881.html

Which outlines how to lock and hide cells with a password. This works great and is what I need. Is there anyway to automate this with VBA?

I am thinking I can unlock cells using this:

    Selection.Locked = False
    Selection.FormulaHidden = False

and then lock selected cells and hide them with:

    Selection.Locked = True
    Selection.FormulaHidden = False
    Selection.EntireRow.Hidden = True

and then protect the worksheet:

   ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

I would like to progress the above to essentially unlock the entire worksheet, then select the rows where there is the word Internal in the cell and then lock and hide them. Finally I would like to lock it all and set the password in VBA (I am not sure if I can do the password bit in VBA?)

The name of the worksheet is 'My Items'.

Any help in joining up the above would be greatly appreciated ;)

Regards,

GISVPN
0
Comment
Question by:gisvpn
[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
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40363382
Hi,

pls try

Sub macro2()
Cells.Locked = False
Cells.FormulaHidden = False

Selection.Locked = True
Selection.EntireRow.Hidden = True

Sheets("my items").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="myPW"

End Sub

Regards
0
 
LVL 12

Accepted Solution

by:
James Elliott earned 500 total points
ID: 40363389
Try this. It assumes your 'Internal' flag is in column A:

Option Explicit

Sub EE()

Dim ws As Worksheet
Dim rng As Range
Dim x As Long

Set ws = ThisWorkbook.Sheets("My Items")
Set rng = ws.Cells

rng.Locked = False

For x = ws.Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1

    Set rng = ws.Range("A" & x)

    If rng = "Internal" Then
        rng.EntireRow.Locked = True
        rng.EntireRow.Hidden = True
    End If
    
Next x

    ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="YourPassword"
    ws.EnableSelection = xlUnlockedCells

End Sub

Open in new window

0
 

Author Comment

by:gisvpn
ID: 40363504
James - thank you again for your input - that's a great automation of the process.

I assume I can build a form to set a custom password each time if wanted?
0
 
LVL 12

Expert Comment

by:James Elliott
ID: 40363508
Option Explicit

Sub EE()

Dim ws As Worksheet
Dim rng As Range
Dim x As Long
Dim sPwd as String

Set ws = ThisWorkbook.Sheets("My Items")
Set rng = ws.Cells

rng.Locked = False

For x = ws.Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1

    Set rng = ws.Range("A" & x)

    If rng = "Internal" Then
        rng.EntireRow.Locked = True
        rng.EntireRow.Hidden = True
    End If
    
Next x

sPwd = InputBox("Please enter password", ThisWorkbook.Name)

    ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:=sPwd
    ws.EnableSelection = xlUnlockedCells

End Sub

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Outlook Free & Paid Tools
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

726 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