Password Protecting/Unprotecting Worksheets using VBA code?

  • Thread starter Thread starter dim
  • Start date Start date
D

dim

Hi folks,

I have a workbook called Book2. A macro opens Book2, inserts and saves user
data, then closes Book2.

This all worked fine until I just tried it out with the password applied to
Book2. the password is "pswd". When the code attempts to insert a row into
Book2's sheet as below, it hangs because it cant insert a row when all the
cells are locked. I thought this might happen, thats why I tested it now. But
I dont know the VBA code I need....

Rows("2:2").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlDown

Can someone please tell me what to add to the code to unprotect the sheet
using the password, then reprotect it before saving and closing?

Thankyou.
 
With Worksheets("Sheet1")
.Unprotect "password"
.Rows(2).Insert
.Protect "password"
End With

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Thanks 'again' Bob,

I hope Im not annoying everyone with all my question, but VBA code is new to
me, and anything I learn I add into my library so I don't have to ask again.

Thanks.
 
Of course not. There is nothing unexpected or adverse in what you do, and we
are free to ignore you <bg>.

Keep asking, we'll keep answering.

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top