宏运行时错误'9':下标超出范围 [英] macro run-time error '9': subscript out of range
本文介绍了宏运行时错误'9':下标超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在网络上找到了一个宏,用于使用密码保护工作表.它工作正常,但是当我保存文件时,出现以下消息:运行时错误'9':订阅超出范围.我以前从未编程或使用过Visual Basic,可以使用一些帮助.谢谢
I found a macro on the web to protect a worksheet with a password. It works fine, but when I save the file I get the message: run-time error '9': subscription out of range. I have never programmed or used visual basic before and could use some help . Thank you
宏是:
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
推荐答案
下标超出范围"表示您已尝试访问不存在的集合中的元素.您的工作簿中是否有"Sheet1"?如果没有,则需要将其更改为要保护的工作表的名称.
"Subscript out of range" indicates that you've tried to access an element from a collection that doesn't exist. Is there a "Sheet1" in your workbook? If not, you'll need to change that to the name of the worksheet you want to protect.
这篇关于宏运行时错误'9':下标超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文