VBA delete rows with worksheet protection

basgrom

New Member
Joined
Jul 26, 2019
Messages
2
Hello,


I use the VBA code below to search and remove a specific row in worksheet shD (Factuur_overzicht) based on the value in worksheet shS (Factuur). It works perfectly without worksheet protection.


Butt the VBA code give an error message when the worksheets are protected. Specific on the deleting part.
I have experimented with AllowDeletingRows and AllowFiltering, but I cannot get it right.


Can you help me with this?


Thanks
Bas

----

Sub Delete_Rows_Based_On_Value()


Dim shS As Worksheet
Dim shD As Worksheet

Set shS = Worksheets("factuur")
Set shD = Worksheets("factuur_overzicht")


'Clear any existing filters
On Error Resume Next
shD.ShowAllData
On Error GoTo 0


'1. Apply Filter
shD.Range("A19:G1000").AutoFilter Field:=2, Criteria1:=shS.Range("L17").Value

'2. Delete Rows
Application.DisplayAlerts = False
shD.Range("A19:G1000").SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True

'3. Clear Filter
On Error Resume Next
shD.ShowAllData
On Error GoTo 0



End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi. Yes you can unprotect the sheet at the top and protect it again once you have deleted. For example

Code:
With Sheets("Sheet1")
    .Unprotect
    .Rows(1).Delete Shift:=xlUp
    .Protect
End With
 
Upvote 0
You need to unprotect the sheet first.
If you need the code for that, just use the recorder whilst you unprotect & than re-protect the sheet.
 
Upvote 0
Thanks, its working now

This does the trick:


--


shD.Unprotect "password"


My vba code


shD.protect "password"
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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