• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Protecting cells once data is entered but still enabling autofilter

s10smith

New Member
Hi Everyone,


I'm a major fan of this website and I thought I could perhaps get some help on something that is stumping me. I have a worksheet that multiple people use and for some columns, once a user enters data I want to lock those cells so that they can't be changed again. I used the following macro:


Private Sub Worksheet_Change(ByVal Target As Range)

Dim MyRange As Range


Set MyRange = Intersect(Range("W10:W9900,AB10:AB9900,AM10:AM9900,AS10:AS9900"), Target)

If Not MyRange Is Nothing Then

Sheets("Sheet1").Unprotect Password:="password"

MyRange.Locked = True

Sheets("Sheet1").Protect Password:="password"

End If

End Sub


Problem is that once the macro runs and protects the worksheet, the autofilter option no longer works.


Any help? (I'm very new to VBA)
 
Welcome to the forum!

When you protect the sheet, need to let XL know that you still want the AutoFilter to work. =)

[pre]
Code:
Sheets("Sheet1").Protect Password:="password", AllowFiltering:=True
[/pre]
 
Hi Luke!


Thanks so much!


This might sound like a silly question but within the macro, would I put the statement you just posted right before the End If?


PS. I hope maybe I can become an excel Ninja!
 
Correct, whole macro becomes:

[pre]
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyRange As Range

Set MyRange = Intersect(Range("W10:W9900,AB10:AB9900,AM10:AM9900,AS10:AS9900"), Target)
If Not MyRange Is Nothing Then
Sheets("Sheet1").Unprotect Password:="password"
MyRange.Locked = True
Sheets("Sheet1").Protect Password:="password", AllowFiltering:=True
End If
End Sub
[/pre]
 
Back
Top