Macro won't work when Protect Sheet is on

smichael

New Member
Joined
Jul 15, 2011
Messages
38
I have a macro I have been using to search throughout data, but it doesn't seem to work with a protected sheet.

"Run-time error '1004':
You cannot use this command on a protected sheet. To use this....." pops up when trying to use.

Here is my code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A3")) Is Nothing Then
    If Range("A3") = "" Then
        On Error Resume Next ' in case autofilter not applied
        ActiveSheet.ShowAllData
    Else
        Range("A7:G1752").AutoFilter Field:=8, Criteria1:="TRUE"
    End If
End If
End Sub

Is there any way to do this with a protected sheet?
Thanks!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try like this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect password:="pw"
If Not Intersect(Target, Range("A3")) Is Nothing Then
    If Range("A3") = "" Then
        On Error Resume Next ' in case autofilter not applied
        ActiveSheet.ShowAllData
    Else
        Range("A7:G1752").AutoFilter Field:=8, Criteria1:="TRUE"
    End If
End If
Me.Protect password:="pw"
End Sub

change pw to the actual password.
 
Upvote 0
Thanks for the quick reply! This does work, but is it possible to do this while others cannot edit the data in the range (A7:G1752) ?
 
Upvote 0
It re-protects the sheet at the end. Users won't be able to edit anything whilst the code is running.
 
Upvote 0
When I applied that to the code, the search worked fine but I was still able to click and edit the data in the range (and only in that section). Do you have any suggestions on what to do? Thanks again
 
Upvote 0
Maybe like this

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
Me.Unprotect password:="pw"
If Not Intersect(Target, Range("A3")) Is Nothing Then
    If Range("A3") = "" Then
        On Error Resume Next ' in case autofilter not applied
        ActiveSheet.ShowAllData
    Else
        Range("A7:G1752").AutoFilter Field:=8, Criteria1:="TRUE"
    End If
End If
Range("A7:G1752").Locked = True
Me.Protect password:="pw"
End Sub
 
Upvote 0
After Sharing the Sheet, an error pops up whenever a cell is edited saying:

"Run-time error '1004':
Method of 'Unprotect' of object '_Worksheet' failed"

is this able to be corrected?
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,282
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top