在同一工作簿中激活工作表时下标错误9

时间:2019-01-28 08:06:36

标签: excel vba

运行以下代码时出现下标错误9。 该代码试图激活工作表“摘要转换”,但抛出错误9。我确实尝试将工作表更改为其他工作表,仍然是相同的错误。

请帮助伙计们!

Private Sub HideUnhideConvertedSheets_Click()
Application.ScreenUpdating = False
ActiveWorkbook.Unprotect Password:="gato"
ActiveSheet.Unprotect Password:="GORDO"
If (Range("CP").Value = 0) Then
Sheets("Summary Converted").Visible = True
Sheets("Services Converted").Visible = True
Sheets("Proposal Converted").Visible = True
Sheets("Revenue & Collections Converted").Visible = True
Sheets("Unisys HW SW Converted").Visible = True
Sheets("Third Party Converted").Visible = True
Sheets("Other Services & OC Converted").Visible = True
Sheets("Cash Flow Converted").Visible = True
Sheets("Annual P&L Converted").Visible = True
Sheets("Travel Converted").Visible = True
Sheets("CapEx Converted").Visible = True
Sheets("Summary Converted").Activate
Range("CP").Value = 1
HideUnhideConvertedSheets.Caption = "Hide Converted Sheets"
Else
Sheets("Summary Converted").Visible = False
Sheets("Services Converted").Visible = False
Sheets("Proposal Converted").Visible = False
Sheets("Revenue & Collections Converted").Visible = False
Sheets("Unisys HW SW Converted").Visible = False
Sheets("CapEx Converted").Visible = False
Sheets("Third Party Converted").Visible = False
Sheets("Other Services & OC Converted").Visible = False
Sheets("Cash Flow Converted").Visible = False
Sheets("Annual P&L Converted").Visible = False
Sheets("Travel Converted").Visible = False
Range("CP").Value = 0
HideUnhideConvertedSheets.Caption = "Show Converted Sheets"
End If
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=_True, 
Password:="GORDO"
ActiveWorkbook.Protect Password:="gato"
End Sub

1 个答案:

答案 0 :(得分:0)

好吧,调用Sheets("Summary Converted").Visible = True效果很好,因此请尝试使用它代替AcitveWorkbook.Sheets("Summary Converted")(不使用ActiveWorkbook)。

另一建议是使用Worksheets而不是Sheets。工作表是工作表和图表工作表的集合。