【问题标题】:Subscript out of Range and Sheet gets protected下标超出范围且工作表受到保护
【发布时间】:2015-05-12 16:58:31
【问题描述】:
Private Sub cmdsub_Click()
UnProtect
'Copy input values to sheet.
Dim lrow As Long
Dim ws As Worksheet
Dim Lvalue As Date
Lvalue = Now
If Me.cbox4.Value = "pending" Then
Set ws = ThisWorkbook.Sheets("Pending")
Else
Set ws = ThisWorkbook.Sheets("Completed")
End If
lrow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
If Me.txt13.Value = "" Then
Me.txt13.SetFocus
MsgBox "'Mandatory", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox1.Value = "" Then
Me.cbox1.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox2.Value = "" Then
Me.cbox2.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox2.Value = "" Then
Me.cbox2.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt1.Value = "" Then
Me.txt1.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt2.Value = "" Then
Me.txt2.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt3.Value = "" Then
Me.txt3.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt4.Value = "" Then
Me.txt4.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt5.Value = "" Then
Me.txt5.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt6.Value = "" Then
Me.txt6.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.txt7.Value = "" Then
Me.txt7.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
ElseIf Me.cbox4.Value = "" Then
Me.cbox4.SetFocus
MsgBox "'Mandatory...", vbOKOnly, "Required Field"
Exit Sub
End If
With ws
    .Cells(lrow, 1).Value = Me.lbl1.Caption
    .Cells(lrow, 2).Value = Me.txt13.Value
    .Cells(lrow, 3).Value = cbox1.Value
    .Cells(lrow, 4).Value = cbox2.Value
    .Cells(lrow, 5).Value = cbox3.Value
    .Cells(lrow, 6).Value = Me.txt1.Value
    .Cells(lrow, 7).Value = Me.txt2.Value
    .Cells(lrow, 8).Value = Me.txt3.Value
    .Cells(lrow, 9).Value = Me.txt4.Value
    .Cells(lrow, 10).Value = Me.txt5.Value
    .Cells(lrow, 11).Value = Me.txt6.Value
    .Cells(lrow, 12).Value = Me.txt7.Value
    .Cells(lrow, 13).Value = cbox4.Value
    .Cells(lrow, 23).Value = Me.txt15.Value
    .Cells(lrow, 24).Value = "Unknown"
            End With
'Clear input controls.
Me.lbl1.Caption = Now()
cbox1.Value = ""
cbox2.Value = ""
cbox3.Value = ""
Me.txt1.Value = ""
Me.txt2.Value = ""
Me.txt3.Value = ""
Me.txt4.Value = ""
Me.txt5.Value = ""
Me.txt6.Value = ""
Me.txt7.Value = ""
Me.txt13.Value = ""
Me.txt15.Value = ""
cbox4.Value = ""
Cmb1.Value = ""
MoveAndDelete
cbox1.Visible = False
cbox2.Visible = False
cbox3.Visible = False
txt1.Visible = False
txt2.Visible = False
txt3.Visible = False
txt4.Visible = False
txt5.Visible = False
txt6.Visible = False
txt7.Visible = False
cbox4.Visible = False
txt13.Visible = False
Label1.Visible = False
Label2.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label8.Visible = False
Label9.Visible = False
Label10.Visible = False
Label11.Visible = False
Label12.Visible = False
txt15.Visible = False
Label21.Visible = False
cmdsub.Visible = False
cmdnxt.Visible = False
abc
bcd
Protect
Application.DisplayAlerts = False
    End Sub

  Sub UnProtect()
  Dim wSheet As Worksheet
   For Each wSheet In Worksheets
    wSheet.UnProtect Password:="Passworld"
  Next wSheet
  End Sub

 Sub Protect()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
    wSheet.Protect Password:="Passworld"
Next wSheet
 End Sub

当我运行上面的代码时,它给了我一个错误:

运行时错误9。下标超出范围。

我无法调试它。我尝试了几种可能性,但没有用。我也有这个保护和取消保护工作表,当我使用这个宏时,我还有其他工作表,它也受到保护。

【问题讨论】:

  • 你能告诉我们哪一行引发了错误,它的细节是什么?
  • 它不会引发任何错误,但是当我尝试添加或复制某些内容时,工作表会自动受到保护,它会给出工作表受保护的错误。
  • 工作表受到保护,因为您在代码末尾有这个Protect。如果您不希望它受到保护,则只需删除此行
  • 对于调试,几乎在每一行都使用断点,当它通过时,去掉之前的断点。您将在哪一行看到错误(这是特定于超出范围的错误,因为它们并不总是在一行上指定)
  • 当您说“无法调试”时,您的意思是您无法通过调试解决问题,还是调试器无法正常工作?

标签: vba excel excel-2010 subscript


【解决方案1】:

检查 lrow 值设置 (ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row) 偏移函数中的 0 可能会导致您的问题,将其删除不是必需的。

【讨论】:

  • 我尝试从偏移量中删除 0,但仍然遇到相同的错误。运行时错误 9.
  • 如果第一行是第 1 行,也可能会出错。这将导致 lrow 为 0,从而导致 .Cells(lrow, 1) 中的错误。您的程序的一部分。但这不会妨碍您调试代码。确定没有办法调试代码?
  • 还有什么可以做的。由于数据从表头后的第二行开始。
  • lrow = ws.UsedRange.Cells(1, 1).Offset(RowOffset:=1).Row 之后检查 lrow 是否不为 0,因为这会在以后引起问题
  • 感谢 Tom 提供上述代码,能否请您帮我处理保护和取消保护代码。此外,我得到了定义为 1004 的错误应用程序。
猜你喜欢
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 2016-11-27
  • 1970-01-01
  • 2016-10-06
  • 2020-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多