How do I password protect all cells dynamically except the header row via VBA

How do I lock all cells in a worksheet except the Header row using VBA.  Note when I run my code I turn off password protection and then turn it back on at the end of the code.  However, the row count is not static, thus today I may have 100 rows and all 100 rows need to be password protected, but the following day there are 200 rows and all 200 rows need to be password protected
upobDaPlayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wayne Taylor (webtubbs)Commented:
Why leave the header row unlocked? Is it allowed to be modified?

In any case, by default, every cell in a worksheet is locked. To unlock a range of cells, use this command...

Rows("1:1").Locked = False
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
upobDaPlayaAuthor Commented:
Actually I do wan the header row locked - but I need to allow for filtering..  Is that possible ?
0
Wayne Taylor (webtubbs)Commented:
Yes, you just need to set the AllowFiltering argument to True...

Sheet1.Protect Password:="password", AllowFiltering:=True
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

upobDaPlayaAuthor Commented:
Exactly what I needed, thank you Wayne including the follow-up...
0
Rob HensonFinance AnalystCommented:
In addition, are you aware that you can protect sheets such that macros can still run without removing the protection.

This level of protection can't be done manually, it has to be done with code with the following line:

Sheet1.Protect Password:="password", UserInterFaceOnly:=True, AllowFiltering:=True
0
upobDaPlayaAuthor Commented:
Rob,
I was not aware of this....if I were to use this code would this code be placed in the start of the code...
0
Rob HensonFinance AnalystCommented:
You would use this as part of separate code for applying the protection. The other code will still run when protected like this.
0
upobDaPlayaAuthor Commented:
Hi Rob, can you define separate piece of code ?   So if I have a module call RunFileDownloads where would I run your line of code ?  Would it be a separate module that I call from RunFileDownloads
0
Rob HensonFinance AnalystCommented:
It would be a completely separate macro that is used purely for protecting (and unprotecting if required) the sheets.

Sub Protect_Sheet()

Sheet1.Protect Password:="password", UserInterFaceOnly:=True, AllowFiltering:=True

End Sub


Sub UnProtect_Sheet()

Sheet1.Unprotect Password:="password"

End Sub

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.