Solved

Using VBA to Hide and Lock Cells with a password

Posted on 2014-10-06
4
184 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
  • 2
4 Comments
 
LVL 48

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

My experience with Windows 10 over a one year period and suggestions for smooth operation
Outlook Free & Paid Tools
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now