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
Solved

Using VBA to Hide and Lock Cells with a password

Posted on 2014-10-06
4
191 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 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

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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!
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

860 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