VBA Advanced Filter Code not Running on protected sheet

tazmtiger

Board Regular
Joined
Jul 7, 2005
Messages
194
Hi!

When I protect worksheet, code below does not work.

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = True
On Error GoTo errHandler
Dim wsD As Worksheet
Dim wsC As Worksheet
Set wsD = Worksheets("Data Search")
Set wsC = Worksheets("Part Entry")
If Target.Cells.Count > 1 Then Exit Sub
If Target.Address = "$B$3" Or Target.Address = "$C$3" Or Target.Address = "$H$3" Then
wsC.Range("Database") _
.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=wsD.Range("B2:H3"), _
CopyToRange:=wsD.Range("A6:K6"), _
Unique:=False
End If

Exit Sub

errHandler:
Application.EnableEvents = True
MsgBox "Data was not retrieved"

End Sub

Can some one help me protect worksheet and still make code work?

Thank you!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
At the beginning of the code un-protect the sheet, and at the end re-protect it.
Code:
Sub blahblah()
Sheets("Sheet1").Unprotect Password:="your_pass"
..
other code
..
Sheets("Sheet1").Protect Password:="your_pass"
End Sub
Protect your VBA project with a password if you don't want others readily seeing the sheet password.
 
Upvote 0
Whoops...got auto and advance filter mixed up. Try:

unprotect the sheet @ start, do the filtering, and then reprotect the sheet @ end.
 
Last edited:
Upvote 0
At the beginning of the code un-protect the sheet, and at the end re-protect it.
Code:
Sub blahblah()
Sheets("Sheet1").Unprotect Password:="your_pass"
..
other code
..
Sheets("Sheet1").Protect Password:="your_pass"
End Sub
Protect your VBA project with a password if you don't want others readily seeing the sheet password.[/Q


Thank you!

Your code worked, but not in that way you illustrated it.

It gave me some out range error your way.

But. I modified the placement, and it worked just fine, as illustrated below:

Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("Data Search").Unprotect Password:="Mypassword"
Application.EnableEvents = True
On Error GoTo errHandler
Dim wsD As Worksheet
Dim wsC As Worksheet
Set wsD = Worksheets("Data Search")
Set wsC = Worksheets("Part Entry")
If Target.Cells.Count > 1 Then Exit Sub
If Target.Address = "$B$3" Or Target.Address = "$C$3" Or Target.Address = "$H$3" Then
wsC.Range("Database") _
.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=wsD.Range("B2:H3"), _
CopyToRange:=wsD.Range("A6:K6"), _
Unique:=False

Sheets("Data Search").Protect Password:="Mypassword"

End If

Exit Sub

errHandler:
Application.EnableEvents = True
MsgBox "Data was not retrieved"

End Sub



Thanks a lot!
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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