Macro runtime error "9": index out of range

I found a macro on the Internet to protect a password sheet. It works fine, but when I save the file, I get a message: runtime error "9": the subscription is out of range. I have never programmed or used a visual base language before and could not use some help. Thanks you

Macro:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

'Step 1:Protect the sheet with a password
    Sheets("Sheet1").protect Password:="btfd"

'Step 2: Save the workbook
    ActiveWorkbook.Save
End Sub
+3
source share
3 answers

An out of range subheading indicates that you tried to access an item from a collection that does not exist. Is there a "sheet1" in your book? If not, you need to change this to the name of the worksheet that you want to protect.

+3
source

? Excel . "/ ...", "", " ", " " " ".

+2

, "": , . , Dark Canuck , , :

Sheets("Sheet1").protect Password:="btfd"

, , "Sheet1" . , : " , , : " 9 ": " , :

ActiveWorkbook.Save

, ? , , ?

0

All Articles