Protect Unprotect Sheet and Run-time error '9'

Andresen

New Member
Joined
Nov 10, 2021
Messages
32
Office Version
  1. 365
Platform
  1. Windows
My macro did work yesterday but did not work today.
Can anyone find the problem?

I get Run-time error '9': Subscript out of range when comming to "ThisWorkbook.Worksheets..." in both UnprotectSheet and ProtectSheet macro.

In my first macro I have

Call UnprotectSheet("BOM")
Call ProtectSheet("BOM")

Sub UnprotectSheet(SheetName As String)
ThisWorkbook.Worksheets(SheetName).Unprotect "purchase"
End Sub

Sub ProtectSheet(SheetName As String)
ThisWorkbook.Worksheets(SheetName).Protect Password:="purchase", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=False, AllowInsertingRows _
:=False, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=False, _
AllowDeletingRows:=False, AllowSorting:=False, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You clearly do not have a sheet called BOM in the workbook with the code.
 
Upvote 0
You clearly do not have a sheet called BOM in the workbook with the code.
That is the strange thing.
Yes I have.

To make it even more strange. To try to make a show case.
* I copied the BOM sheet into a new sheet.
* I put over the code as below.
* In the new book it worked. I tried the macro from the new book into my old sheet. The macro started to work in my old sheet when using the macro from the new sheet.
* I closed down everything.
* Opened the new file and the original file. Tried the macro from the new file on the original files BOM sheet. It did not work.
* I removed the BOM sheet in the new file. And again copied over the BOM sheet from the original file to the new file. The macro did work on the BOM sheet in the new file.
* I did again test on the BOM sheet in the original file. And it did not work.

* I wrote the text above in this Trail
* I did again run the macro from the new file in the original file. (I changed so I had one other sheet active). And it worked. I made the BOM sheet active and it worked.
It is very unstable. What shall I look for?
//S



Sub UnprotectAndProtectBOM()
Call UnprotectSheet("BOM")
Call ProtectSheet("BOM")
End Sub

Sub ProtectSheet(SheetName As String)
ThisWorkbook.Worksheets(SheetName).Protect Password:="purchase", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=False, AllowInsertingRows _
:=False, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=False, _
AllowDeletingRows:=False, AllowSorting:=False, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End Sub

Sub UnprotectSheet(SheetName As String)
ThisWorkbook.Worksheets(SheetName).Unprotect "purchase"
End Sub
 
Upvote 0
I have tested little more and found this.

That is the strange thing.
Yes I have.

To make it even more strange.
* I tried to make a show case
* I copied over the macro text
* Made a test macro UnprotectAndProtectBOM
* I copied the BOM sheet into a new sheet.
* In the new book it worked. I tried the macro from the new book into my old sheet. The macro started to work in my old sheet when using the macro from the new sheet.
* I copied and replaced the macros into my old sheets macro modules. The UnprotectAndProtectBOM macro was also added.
* I run the macro from the old sheets macro on the old sheet. Did not work
* I run the macro from the new sheets macro on the old sheet. Did work.
* I run the macro from the old sheets macro on the old sheet. Did not work.

I have two public variables in the old macro module. You can see them below. I cannot see it is affecting.

What shall I look for?
//S



Sub UnprotectAndProtectBOM()
Call UnprotectSheet("BOM")
Call ProtectSheet("BOM")
End Sub

Sub ProtectSheet(SheetName As String)
ThisWorkbook.Worksheets(SheetName).Protect Password:="purchase", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=False, AllowInsertingRows _
:=False, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=False, _
AllowDeletingRows:=False, AllowSorting:=False, AllowFiltering:=True, _
AllowUsingPivotTables:=True
End Sub

Sub UnprotectSheet(SheetName As String)
ThisWorkbook.Worksheets(SheetName).Unprotect "purchase"
End Sub


In the old macro modules I have two public Variables
Public VStop As String
Public Inputsheet As String
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,466
Members
449,086
Latest member
kwindels

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