Refresh Macro Not Working on Protected Sheet

evxret

New Member
Joined
Apr 8, 2022
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Hi all, I have created a macro that I believe is written correctly, It doesn't give any VBA errors rather says I cannot refresh the table because the sheet is protected. However, I have added lines into the macro that specify to unprotect the sheet, refresh, and then re-protect (and in my case, re-add all of my edit ranges.) I run this macro and It gives this error:
1649958609980.png

Heres the code Im using for the refresh button, you can see it unprotects before refreshing, so I'm not sure why it's throwing this error.
VBA Code:
Sub PreSalesRefresh()
 With Sheets("PreSalesForm")
 
          .Unprotect "abc"  'sheet is protected with this password
          
          ThisWorkbook.RefreshAll
          
        Set c = .Range("E3:E9,E15:E16,F15:F16,G15:G16,D20:D21,D35:K60,E20:E21,F20:F21,G20:G21,G30:G31") 'Setting a range variable for edit range.

        On Error Resume Next
        .Protection.AllowEditRanges("MyRange").Delete  'Delete Edit Range In Case It Already Exists
        On Error GoTo 0

        
        .Protection.AllowEditRanges.Add Title:="MyRange", Range:=c  'Re-Add Edit Range w/ Specified Range Variable

        
        .Protect "abc"   'Re-Protects Sheet

     End With
End Sub

 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,214,870
Messages
6,122,019
Members
449,060
Latest member
LinusJE

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