Macro will protect worksheet, but will not unprotect

Danielt949

New Member
Joined
Jun 28, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Afternoon all.

I am having issues with a Macro not unprotecting a worksheet.

If i manually unprotect the worksheet "Dashboard" via the bottom tabs, My macro button will open the userform no dramas.
When i close the userform to test the macro to make sure it has protected "Dashboard" as it should have, it is good.
However if i use the button to open userform again (with "Dashboard") still protected, The button will not unlock it. Coming up with a protection error.
Again, i manually unprotect, test button and it works, The form opens up.
I close the form again and test the "Dashboard"and it is locked again as it should be.
Again i can not open the userform as it wont unprotect.

I know the above is written twice, however this is purely to demonstrate the situation i am going through when testing the macro.
The weirder part is.... The macro is a "Copy and Paste" from a macro that is working, still working and not playing up, the only thing changed is the macro name. (Which is not the same as the module name)

VBA Code:
Sub DefectSearchForm()
With UserDefectSearch

Sheets("Dashboard").Unprotect "1"

  .StartUpPosition = 0
  .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  .Show
  
Sheets("Dashboard").Protect "1"

End With
End Sub

NOTE - I have also tried using ActiveSheet.Unprotect "1" and this did not work.

Please Help

Cheers
 

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.
I ran your code and it seems to work normally for me. It protects and unprotects as expected.
 
Upvote 0
Not sure if this is causing the issue, or not, but it is good practice to completely define sheet variables (in case you have a PERSONAL MACROS or other template that opens with Excel)
VBA Code:
Dim wb as Workbook, sht as Worksheet
Set wb = Workbooks("Your_Workbook_Name.xlsx")
Set sht = wb.Sheets("Dashboard")
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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