VBA // Protection.AllowEditRanges

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
362
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello.

I'd like to delete all editable ranges in the workbook, so I have this simple code:

Code:
Sub Macro4()

Dim wks As Worksheet
Dim aer As AllowEditRange


For Each wks In Worksheets

    For Each aer In wks.Protection.AllowEditRanges
        
        wks.Activate
        aer.Delete
        
    Next aer
    
Next wks


End Sub
But it's stuck on the aer.Delete part giving me "application or object defined error".
I have all sheets unprotected.

Can someone help me fix it?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try unprotecting the worksheet (& re-protecting if required) just inside the start (& end) of the wks loop.
 
Upvote 0
Yeah that helped, thank you!

Although there is one problem with this code.
When I just run it, it goes smoothly, but when I go step by step (F8) I get "Method 'Delete' of object 'AllowEditRange' failed" when it passes wks.Activate and should go through aer.Delete.

Do you by any chance know why it happens?
 
Upvote 0
No, and I haven't been able to reproduce what you are describing. Perhaps it relates to passwords and/or permissions that have been applied to the particular ranges. Or hidden sheets.

In any case, I'm not sure my previous suggestion should have made any difference as I had missed the following comment in your original post:
I have all sheets unprotected
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,777
Members
449,049
Latest member
greyangel23

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